# -----------------------------------------------------------------------
#    Title: Too Tired to Vote: A Multi-National Comparison of Election Turnout with Sleep Preferences and Behaviors
#    Authors: 
#    Date: 03 31 2022 
# -----------------------------------------------------------------------

#### Cleaning the Working Environment ####
rm(list = ls(all=T))
gc()

#### Getting the Working Directory ####
getwd()

#### Creating Empty Folder for Tables in the Working Directory ####
dir.create("Tables")

#### Loading Packages ####
library(haven)
library(sjPlot)
library(nnet)
library(stargazer)
library(tidyverse)
library(viridisLite)
library(viridis)
library(forcats)
library(data.table)
library(openxlsx)
library(psych)
library(lme4)
library(lattice)
library(ggeffects)
library(sjlabelled)
library(naniar)
library(dotwhisker)
library(broom)
library(RColorBrewer)
#library(merTools)
library(ggplot2)
library(openxlsx)
library(apaTables)
library(readxl)
library(ggrepel)

#### Inserting functions ####

qcut2 <- function(x, n) {
  findInterval(x, quantile(x, seq(0, 1, length = n + 1), na.rm=T), all.inside = T)
}


#### Loading DATASETS ####
issp_2007 <- read_dta("Datasets/ZA4850_v2-0-0.dta")
issp_2007$V5 <- as.numeric(issp_2007$V5)

#### Checking and Preparing Sleep Variables in the DATASETS ####
table(issp_2007$V67, exclude = NULL)
table(issp_2007$V68, exclude = NULL)

issp_2007$sleeping_time_hour_alternative <- substr(as.POSIXct(sprintf("%04.0f", issp_2007$V68), format='%H%M'), 12, 16)
issp_2007$getting_up_time_hour_alternative <- substr(as.POSIXct(sprintf("%04.0f", issp_2007$V67), format='%H%M'), 12, 16)

table(issp_2007$sleeping_time_hour_alternative, exclude = NULL)
table(issp_2007$getting_up_time_hour_alternative, exclude = NULL)

issp_2007$Sleeping_Hour <- format(as.POSIXct(issp_2007$sleeping_time_hour_alternative, format="%H:%M"),"%H")
issp_2007$Sleeping_Minute <- format(as.POSIXct(issp_2007$sleeping_time_hour_alternative, format="%H:%M"),"%M")

issp_2007$Waking_Hour <- format(as.POSIXct(issp_2007$getting_up_time_hour_alternative, format="%H:%M"),"%H")
issp_2007$Waking_Minute <- format(as.POSIXct(issp_2007$getting_up_time_hour_alternative, format="%H:%M"),"%M")

issp_2007$sleeping_time_hour_alternative <- as.numeric(issp_2007$Sleeping_Hour) + as.numeric(issp_2007$Sleeping_Minute)/60
issp_2007$getting_up_time_hour_alternative <- as.numeric(issp_2007$Waking_Hour) + as.numeric(issp_2007$Waking_Minute)/60

#### Loading Netherlands Dataset ####
issp_2007_netherlands <- read_dta("Datasets/issp_2007nl_def.dta")

issp_2007_netherlands$nl_hinc <- ifelse(issp_2007_netherlands$nl_hinc==99999, NA, issp_2007_netherlands$nl_hinc)
issp_2007_netherlands$NL_INC <- issp_2007_netherlands$nl_hinc
table(issp_2007_netherlands$NL_INC, exclude = NULL)
str(issp_2007_netherlands$NL_INC)
issp_2007_netherlands$NL_INC <- as.numeric(issp_2007_netherlands$NL_INC)
table(qcut2(issp_2007_netherlands$NL_INC, 5), exclude = NULL)
issp_2007_netherlands$NL_INC_rank <- qcut2(issp_2007_netherlands$NL_INC, 5)
table(issp_2007_netherlands$NL_INC_rank, exclude = NULL)

issp_2007_netherlands$V58 <- ifelse(issp_2007_netherlands$V58==-1 | issp_2007_netherlands$V58==8, NA, issp_2007_netherlands$V58)
table(issp_2007_netherlands$V58, exclude = NULL)

issp_2007_netherlands$V66 <- ifelse(issp_2007_netherlands$V66==-1, NA, issp_2007_netherlands$V66)
table(issp_2007_netherlands$V66, exclude = NULL)

issp_2007_netherlands$V67 <- ifelse(issp_2007_netherlands$V67==-1, NA, issp_2007_netherlands$V67)
table(issp_2007_netherlands$V67, exclude = NULL)

issp_2007_netherlands$V68 <- ifelse(issp_2007_netherlands$V68==-1, NA, issp_2007_netherlands$V68)
table(issp_2007_netherlands$V68, exclude = NULL)

issp_2007_netherlands$degree <- ifelse(issp_2007_netherlands$degree==9, NA, issp_2007_netherlands$degree)
table(issp_2007_netherlands$degree, exclude = NULL)

issp_2007_netherlands$nl_hinc <- ifelse(issp_2007_netherlands$nl_hinc==99999, NA, issp_2007_netherlands$nl_hinc)
table(issp_2007_netherlands$nl_hinc, exclude = NULL)

issp_2007_netherlands$PARTY_LR <- ifelse(issp_2007_netherlands$PARTY_LR==7 | issp_2007_netherlands$PARTY_LR==9, NA, issp_2007_netherlands$PARTY_LR)
table(issp_2007_netherlands$PARTY_LR, exclude = NULL)

issp_2007_netherlands$attend <- ifelse(issp_2007_netherlands$attend==98 | issp_2007_netherlands$attend==99, NA, issp_2007_netherlands$attend)
table(issp_2007_netherlands$attend, exclude = NULL)

table(issp_2007_netherlands$relig, exclude = NULL)
issp_2007_netherlands$relig <- ifelse(issp_2007_netherlands$relig==0, NA, issp_2007_netherlands$relig)

table(issp_2007_netherlands$religgrp, exclude = NULL)
issp_2007_netherlands$religgrp <- ifelse(issp_2007_netherlands$religgrp==99, NA, issp_2007_netherlands$religgrp)

#### Checking and Preparing Sleep Variables in the Netherlands Dataset ####

issp_2007_netherlands$V67 <- as.numeric(issp_2007_netherlands$V67)
table(issp_2007_netherlands$V67, exclude = NULL)
issp_2007_netherlands$V67 <- issp_2007_netherlands$V67*100

issp_2007_netherlands$V68 <- as.numeric(issp_2007_netherlands$V68)
table(issp_2007_netherlands$V68, exclude = NULL)
issp_2007_netherlands$V68 <- issp_2007_netherlands$V68*100

issp_2007_netherlands$sleeping_time_hour_alternative <- substr(as.POSIXct(sprintf("%04.0f", issp_2007_netherlands$V68), format='%H%M'), 12, 16)
issp_2007_netherlands$getting_up_time_hour_alternative <- substr(as.POSIXct(sprintf("%04.0f", issp_2007_netherlands$V67), format='%H%M'), 12, 16)

table(issp_2007_netherlands$sleeping_time_hour_alternative, exclude = NULL)
table(issp_2007_netherlands$getting_up_time_hour_alternative, exclude = NULL)

issp_2007_netherlands$Sleeping_Hour <- format(as.POSIXct(issp_2007_netherlands$sleeping_time_hour_alternative, format="%H:%M"),"%H")
issp_2007_netherlands$Sleeping_Minute <- format(as.POSIXct(issp_2007_netherlands$sleeping_time_hour_alternative, format="%H:%M"),"%M")

issp_2007_netherlands$Waking_Hour <- format(as.POSIXct(issp_2007_netherlands$getting_up_time_hour_alternative, format="%H:%M"),"%H")
issp_2007_netherlands$Waking_Minute <- format(as.POSIXct(issp_2007_netherlands$getting_up_time_hour_alternative, format="%H:%M"),"%M")

issp_2007_netherlands$sleeping_time_hour_alternative <- as.numeric(issp_2007_netherlands$Sleeping_Hour) + as.numeric(issp_2007_netherlands$Sleeping_Minute)/60
issp_2007_netherlands$getting_up_time_hour_alternative <- as.numeric(issp_2007_netherlands$Waking_Hour) + as.numeric(issp_2007_netherlands$Waking_Minute)/60

#### Merging ISSP 2007 and the Netherlands Datasets ####
issp_2007 <- dplyr::bind_rows(issp_2007, issp_2007_netherlands)

table(issp_2007$religgrp, exclude = NULL)

issp_2007$religious_denomination <- NA
issp_2007$religious_denomination[issp_2007$religgrp==1] <- "No_religion"
issp_2007$religious_denomination[issp_2007$religgrp==2] <- "Roman_Catholic"
issp_2007$religious_denomination[issp_2007$religgrp==3] <- "Protestant"
issp_2007$religious_denomination[issp_2007$religgrp==4] <- "Christian_Orthodox"
issp_2007$religious_denomination[issp_2007$religgrp==5] <- "Jewish"
issp_2007$religious_denomination[issp_2007$religgrp==6] <- "Islam"
issp_2007$religious_denomination[issp_2007$religgrp==7] <- "Buddhism"
issp_2007$religious_denomination[issp_2007$religgrp==8] <- "Hinduism"
issp_2007$religious_denomination[issp_2007$religgrp==9] <- "Other_Christian_Religions"
issp_2007$religious_denomination[issp_2007$religgrp==10] <- "Other_Eastern_Religions"
issp_2007$religious_denomination[issp_2007$religgrp==11] <- "Other_Religions"
table(issp_2007$religious_denomination, exclude = NULL)

issp_2007 <- fastDummies::dummy_cols(issp_2007, select_columns = 'religious_denomination')

table(issp_2007$religious_denomination_Buddhism, exclude = NULL)
issp_2007$religious_denomination_Buddhism_avg <- mean(issp_2007$religious_denomination_Buddhism, na.rm = T)
issp_2007$religious_denomination_Buddhism_beta <- (issp_2007$religious_denomination_Buddhism-issp_2007$religious_denomination_Buddhism_avg)
table(issp_2007$religious_denomination_Buddhism_beta, exclude = NULL)

table(issp_2007$religious_denomination_Christian_Orthodox, exclude = NULL)
issp_2007$religious_denomination_Christian_Orthodox_avg <- mean(issp_2007$religious_denomination_Christian_Orthodox, na.rm = T)
issp_2007$religious_denomination_Christian_Orthodox_beta <- (issp_2007$religious_denomination_Christian_Orthodox-issp_2007$religious_denomination_Christian_Orthodox_avg)
table(issp_2007$religious_denomination_Christian_Orthodox_beta, exclude = NULL)

table(issp_2007$religious_denomination_Hinduism, exclude = NULL)
issp_2007$religious_denomination_Hinduism_avg <- mean(issp_2007$religious_denomination_Hinduism, na.rm = T)
issp_2007$religious_denomination_Hinduism_beta <- (issp_2007$religious_denomination_Hinduism-issp_2007$religious_denomination_Hinduism_avg)
table(issp_2007$religious_denomination_Hinduism_beta, exclude = NULL)

table(issp_2007$religious_denomination_Islam, exclude = NULL)
issp_2007$religious_denomination_Islam_avg <- mean(issp_2007$religious_denomination_Islam, na.rm = T)
issp_2007$religious_denomination_Islam_beta <- (issp_2007$religious_denomination_Islam-issp_2007$religious_denomination_Islam_avg)
table(issp_2007$religious_denomination_Islam_beta, exclude = NULL)

table(issp_2007$religious_denomination_Jewish, exclude = NULL)
issp_2007$religious_denomination_Jewish_avg <- mean(issp_2007$religious_denomination_Jewish, na.rm = T)
issp_2007$religious_denomination_Jewish_beta <- (issp_2007$religious_denomination_Jewish-issp_2007$religious_denomination_Jewish_avg)
table(issp_2007$religious_denomination_Jewish_beta, exclude = NULL)

table(issp_2007$religious_denomination_No_religion, exclude = NULL)
issp_2007$religious_denomination_No_religion_avg <- mean(issp_2007$religious_denomination_No_religion, na.rm = T)
issp_2007$religious_denomination_No_religion_beta <- (issp_2007$religious_denomination_No_religion-issp_2007$religious_denomination_No_religion_avg)
table(issp_2007$religious_denomination_No_religion_beta, exclude = NULL)

table(issp_2007$religious_denomination_Other_Christian_Religions, exclude = NULL)
issp_2007$religious_denomination_Other_Christian_Religions_avg <- mean(issp_2007$religious_denomination_Other_Christian_Religions, na.rm = T)
issp_2007$religious_denomination_Other_Christian_Religions_beta <- (issp_2007$religious_denomination_Other_Christian_Religions-issp_2007$religious_denomination_Other_Christian_Religions_avg)
table(issp_2007$religious_denomination_Other_Christian_Religions_beta, exclude = NULL)

table(issp_2007$religious_denomination_Other_Eastern_Religions, exclude = NULL)
issp_2007$religious_denomination_Other_Eastern_Religions_avg <- mean(issp_2007$religious_denomination_Other_Eastern_Religions, na.rm = T)
issp_2007$religious_denomination_Other_Eastern_Religions_beta <- (issp_2007$religious_denomination_Other_Eastern_Religions-issp_2007$religious_denomination_Other_Eastern_Religions_avg)
table(issp_2007$religious_denomination_Other_Eastern_Religions_beta, exclude = NULL)

table(issp_2007$religious_denomination_Other_Religions, exclude = NULL)
issp_2007$religious_denomination_Other_Religions_avg <- mean(issp_2007$religious_denomination_Other_Religions, na.rm = T)
issp_2007$religious_denomination_Other_Religions_beta <- (issp_2007$religious_denomination_Other_Religions-issp_2007$religious_denomination_Other_Religions_avg)
table(issp_2007$religious_denomination_Other_Religions_beta, exclude = NULL)

table(issp_2007$religious_denomination_Protestant, exclude = NULL)
issp_2007$religious_denomination_Protestant_avg <- mean(issp_2007$religious_denomination_Protestant, na.rm = T)
issp_2007$religious_denomination_Protestant_beta <- (issp_2007$religious_denomination_Protestant-issp_2007$religious_denomination_Protestant_avg)
table(issp_2007$religious_denomination_Protestant_beta, exclude = NULL)

table(issp_2007$religious_denomination_Roman_Catholic, exclude = NULL)
issp_2007$religious_denomination_Roman_Catholic_avg <- mean(issp_2007$religious_denomination_Roman_Catholic, na.rm = T)
issp_2007$religious_denomination_Roman_Catholic_beta <- (issp_2007$religious_denomination_Roman_Catholic-issp_2007$religious_denomination_Roman_Catholic_avg)
table(issp_2007$religious_denomination_Roman_Catholic_beta, exclude = NULL)

#### Dropping Observations with Missing Sleep Variables ####
issp_2007 <- subset(issp_2007, subset = (!is.na(issp_2007$V67) | !is.na(issp_2007$V68)))

#### Calculating Duration of Sleep ####
issp_2007$sleep_duration_alternative <-  (issp_2007$getting_up_time_hour_alternative-issp_2007$sleeping_time_hour_alternative) 
table(issp_2007$sleep_duration_alternative, exclude = NULL)
issp_2007$sleep_duration_alternative <-  ifelse(issp_2007$getting_up_time_hour_alternative>=issp_2007$sleeping_time_hour_alternative, issp_2007$sleep_duration_alternative, (24 - issp_2007$sleeping_time_hour_alternative) + issp_2007$getting_up_time_hour_alternative) 
table(issp_2007$sleep_duration_alternative, exclude = NULL)

#### Calculating Chronotype ####
issp_2007$mid_point_alternative <- issp_2007$getting_up_time_hour_alternative - issp_2007$sleep_duration_alternative/2
issp_2007$mid_point_alternative <- ifelse(issp_2007$mid_point_alternative<0, issp_2007$mid_point_alternative+24, issp_2007$mid_point_alternative)
table(issp_2007$mid_point_alternative)

#### Exclusion Criteria based on Duration of Sleep and Chronotype ####
issp_2007 <- subset(issp_2007, subset = (issp_2007$sleeping_time_hour_alternative<=6 | issp_2007$sleeping_time_hour_alternative>=19) & (issp_2007$getting_up_time_hour_alternative<=15 & issp_2007$getting_up_time_hour_alternative>=4))
issp_2007 <- subset(issp_2007, subset = issp_2007$sleep_duration_alternative>=2 & sleep_duration_alternative<=18)

#### Reverse Coding Chronotype - Bigger Values for Morningness and Smaller Values for Eveningness ####
table(issp_2007$mid_point_alternative, exclude = NULL)
issp_2007$ORIGINAL_MID_POINT <- issp_2007$mid_point_alternative
issp_2007$midcenter <- issp_2007$mid_point_alternative-20
table(issp_2007$midcenter, exclude = NULL)
issp_2007$cond3 <- issp_2007$midcenter + 24
issp_2007$midcenter <- ifelse(issp_2007$midcenter<0, issp_2007$cond3, issp_2007$midcenter)
table(issp_2007$midcenter, exclude = NULL)
issp_2007$mid_point_alternative <- issp_2007$midcenter
table(issp_2007$mid_point_alternative, exclude = NULL)
issp_2007$chronotype_interval <-  issp_2007$mid_point_alternative*-1
issp_2007$sleep_duration <-  issp_2007$sleep_duration_alternative
table(issp_2007$chronotype_interval, exclude = NULL)
table(issp_2007$sleep_duration, exclude = NULL)

#### Checking and Prepating Covariates ####
table(issp_2007$degree, exclude = NULL)
issp_2007$degree <- ifelse(issp_2007$degree>5, NA, issp_2007$degree)

table(issp_2007$sex, exclude = NULL) # 1 - male, 2 - female
issp_2007$sex <- issp_2007$sex
issp_2007$sex[issp_2007$sex==1] <- 1
issp_2007$sex[issp_2007$sex==2] <- 0
table(issp_2007$sex, exclude = NULL)


table(issp_2007$V66, exclude = NULL)
issp_2007$day_off <- NA
issp_2007$day_off[issp_2007$V66==2] <- 1 ## Weekend
issp_2007$day_off[issp_2007$V66==1] <- 0 ## Weekday
table(issp_2007$day_off, exclude = NULL)


table(issp_2007$V58, exclude = NULL)
issp_2007$political_interest <- issp_2007$V58
table(issp_2007$political_interest, exclude = NULL)
issp_2007$political_interest <- ifelse(issp_2007$political_interest>4, NA, issp_2007$political_interest)
issp_2007$political_interest <- 5 - issp_2007$political_interest

table(issp_2007$urbrural, exclude = NULL)
issp_2007$urban_rural_area <- NA
issp_2007$urban_rural_area[issp_2007$urbrural==5 | issp_2007$urbrural==4] <- 1 # to match with Greece data
issp_2007$urban_rural_area[issp_2007$urbrural==3] <- 2
issp_2007$urban_rural_area[issp_2007$urbrural==2] <- 3
issp_2007$urban_rural_area[issp_2007$urbrural==1] <- 4
table(issp_2007$urban_rural_area, exclude = NULL)

table(issp_2007$attend, exclude = NULL)

issp_2007$religiosity <- NA
issp_2007$religiosity[issp_2007$attend==8] <- 1
issp_2007$religiosity[issp_2007$attend==7 | issp_2007$attend==6] <- 2
issp_2007$religiosity[issp_2007$attend==5] <- 3
issp_2007$religiosity[issp_2007$attend==4] <- 4
issp_2007$religiosity[issp_2007$attend==3] <- 5
issp_2007$religiosity[issp_2007$attend==1 | issp_2007$attend==2] <- 6
table(issp_2007$religiosity, exclude = NULL)

issp_2007$age <- ifelse(issp_2007$age<18, NA, issp_2007$age)

issp_2007$PARTY_LR <- ifelse(issp_2007$PARTY_LR>5, NA, issp_2007$PARTY_LR)
table(issp_2007$PARTY_LR, exclude = NULL)

table(issp_2007$VOTE_LE, exclude = NULL)
issp_2007$turnout <- NA
issp_2007$turnout[issp_2007$VOTE_LE==2] <- 0
issp_2007$turnout[issp_2007$VOTE_LE==1] <- 1
table(issp_2007$turnout, exclude = NULL)

#### Loading Greece Dataset ####

greece_data <- read_dta("Datasets/Greece_2020.dta")

greece_data$V67 <-  greece_data$hourtime1 + greece_data$minutetime1/100
table(greece_data$V67, exclude = NULL)
greece_data$V67 <- greece_data$V67*100

greece_data$V68 <-  greece_data$hourtime2_corrected + greece_data$minutetime2/100
table(greece_data$V68, exclude = NULL)
greece_data$V68 <- greece_data$V68*100

greece_data$sleeping_time_hour_alternative <- substr(as.POSIXct(sprintf("%04.0f", greece_data$V68), format='%H%M'), 12, 16)
greece_data$getting_up_time_hour_alternative <- substr(as.POSIXct(sprintf("%04.0f", greece_data$V67), format='%H%M'), 12, 16)

table(greece_data$sleeping_time_hour_alternative, exclude = NULL)
table(greece_data$getting_up_time_hour_alternative, exclude = NULL)

greece_data$Sleeping_Hour <- format(as.POSIXct(greece_data$sleeping_time_hour_alternative, format="%H:%M"),"%H")
greece_data$Sleeping_Minute <- format(as.POSIXct(greece_data$sleeping_time_hour_alternative, format="%H:%M"),"%M")

greece_data$Waking_Hour <- format(as.POSIXct(greece_data$getting_up_time_hour_alternative, format="%H:%M"),"%H")
greece_data$Waking_Minute <- format(as.POSIXct(greece_data$getting_up_time_hour_alternative, format="%H:%M"),"%M")

greece_data$sleeping_time_hour_alternative <- as.numeric(greece_data$Sleeping_Hour) + as.numeric(greece_data$Sleeping_Minute)/60
greece_data$getting_up_time_hour_alternative <- as.numeric(greece_data$Waking_Hour) + as.numeric(greece_data$Waking_Minute)/60

greece_data$sleep_duration_alternative <-  (greece_data$getting_up_time_hour_alternative-greece_data$sleeping_time_hour_alternative) 
table(greece_data$sleep_duration_alternative, exclude = NULL)
greece_data$sleep_duration_alternative <-  ifelse(greece_data$getting_up_time_hour_alternative>=greece_data$sleeping_time_hour_alternative, greece_data$sleep_duration_alternative, (24 - greece_data$sleeping_time_hour_alternative) + greece_data$getting_up_time_hour_alternative) 
table(greece_data$sleep_duration_alternative, exclude = NULL)

greece_data$mid_point_alternative <- greece_data$getting_up_time_hour_alternative - greece_data$sleep_duration_alternative/2
greece_data$mid_point_alternative <- ifelse(greece_data$mid_point_alternative<0, greece_data$mid_point_alternative+24, greece_data$mid_point_alternative)
table(greece_data$mid_point_alternative)

greece_data <- subset(greece_data, subset = (greece_data$sleeping_time_hour_alternative<=6 | greece_data$sleeping_time_hour_alternative>=19) & (greece_data$getting_up_time_hour_alternative<=15 & greece_data$getting_up_time_hour_alternative>=4))
greece_data <- subset(greece_data, subset = greece_data$sleep_duration_alternative>=2 & sleep_duration_alternative<=18)

table(greece_data$mid_point_alternative, exclude = NULL)
greece_data$ORIGINAL_MID_POINT <- greece_data$mid_point_alternative
greece_data$midcenter <- greece_data$mid_point_alternative-20
table(greece_data$midcenter, exclude = NULL)
greece_data$cond3 <- greece_data$midcenter + 24
greece_data$midcenter <- ifelse(greece_data$midcenter<0, greece_data$cond3, greece_data$midcenter)
table(greece_data$midcenter, exclude = NULL)
greece_data$mid_point_alternative <- greece_data$midcenter
table(greece_data$mid_point_alternative, exclude = NULL)
greece_data$chronotype_interval <-  greece_data$mid_point_alternative*-1
greece_data$sleep_duration <-  greece_data$sleep_duration_alternative
table(greece_data$chronotype_interval, exclude = NULL)
table(greece_data$sleep_duration, exclude = NULL)

greece_data$degree <- NA
greece_data$degree[greece_data$D03f==1] <- 0
greece_data$degree[greece_data$D03f==2 | greece_data$D03f==3] <- 1
greece_data$degree[greece_data$D03f==4 | greece_data$D03f==5] <- 2
greece_data$degree[greece_data$D03f==6] <- 3
greece_data$degree[greece_data$D03f==7] <- 4
greece_data$degree[greece_data$D03f==8 | greece_data$D03f==9 | greece_data$D03f==10] <- 5
table(greece_data$degree, exclude = NULL)

greece_data$sex <- NA
greece_data$sex[greece_data$D02f==1] <- 1
greece_data$sex[greece_data$D02f==2] <- 0
table(greece_data$sex, exclude = NULL)

greece_data$interview_time <- paste(greece_data$qyear, greece_data$qmonth, greece_data$qday, sep = "-")
table(greece_data$interview_time, exclude = NULL)

greece_data$day_off <- NA
greece_data$day_off[greece_data$interview_time=="2019-12-14" |
                      greece_data$interview_time=="2019-12-15" |
                      greece_data$interview_time== "2019-12-22" |
                      greece_data$interview_time=="2019-12-28" |
                      greece_data$interview_time=="2019-12-29" |
                      greece_data$interview_time== "2020-1-4" |
                      greece_data$interview_time=="2020-1-5" |
                      greece_data$interview_time== "2020-2-1" |
                      greece_data$interview_time== "2020-2-2" |
                      greece_data$interview_time=="2020-2-8" |
                      greece_data$interview_time=="2020-2-9" |
                      greece_data$interview_time=="2020-2-15" |
                      greece_data$interview_time== "2020-2-16"] <- 1
greece_data$day_off <- ifelse(is.na(greece_data$day_off), 0, greece_data$day_off)
table(greece_data$day_off, exclude = NULL)

table(greece_data$Q01f, exclude = NULL)
greece_data$political_interest <- greece_data$Q01f
table(greece_data$political_interest, exclude = NULL)
greece_data$political_interest <- ifelse(greece_data$political_interest>4, NA, greece_data$political_interest)
greece_data$political_interest <- 5 - greece_data$political_interest

greece_data$urban_rural_area <- greece_data$D19f
greece_data$urban_rural_area <- ifelse(greece_data$urban_rural_area==5, NA, greece_data$urban_rural_area)
table(greece_data$urban_rural_area, exclude = NULL)

table(greece_data$D11f, exclude = NULL)
greece_data$religiosity <- greece_data$D11f
greece_data$religiosity <- ifelse(greece_data$religiosity==7, NA, greece_data$religiosity)
table(greece_data$religiosity, exclude = NULL)

table(greece_data$Q18f, exclude = NULL)
greece_data$ideology <- greece_data$Q18f
greece_data$ideology <- ifelse(greece_data$ideology==12, NA, greece_data$ideology)
table(greece_data$ideology, exclude = NULL)
greece_data$PARTY_LR <- (5-1)/(11-1)*(greece_data$ideology-11) + 5
table(greece_data$PARTY_LR, exclude = NULL)
greece_data$PARTY_LR <- round(greece_data$PARTY_LR, 0)
greece_data$PARTY_LR[greece_data$ideology==5] <- 2
greece_data$PARTY_LR[greece_data$ideology==7] <- 4
table(greece_data$PARTY_LR, greece_data$ideology, exclude = NULL)
table(greece_data$PARTY_LR, exclude = NULL)

table(greece_data$age, exclude = NULL)
greece_data$age <- ifelse(greece_data$age<18, NA, greece_data$age)
greece_data <- subset(greece_data, subset = !is.na(age))

table(greece_data$Q12LHaf, exclude = NULL)
greece_data$turnout <- NA
greece_data$turnout[greece_data$Q12LHaf==2] <- 0
greece_data$turnout[greece_data$Q12LHaf==1] <- 1
table(greece_data$turnout, exclude = NULL)

table(greece_data$D09f, exclude = NULL)
str(greece_data$D09f)
greece_data$D09f <- as.numeric(greece_data$D09f)
greece_data$GR_INC_rank <- greece_data$D09f
greece_data$GR_INC_rank <- ifelse(greece_data$GR_INC_rank==6, NA, greece_data$GR_INC_rank)
table(greece_data$GR_INC_rank, exclude = NULL)

greece_data$V5 <- 2020

issp_2007 <- dplyr::bind_rows(issp_2007, greece_data)

#### Income variable for all ####

table(issp_2007$FI_INC, exclude = NULL)
str(issp_2007$FI_INC)
issp_2007$FI_INC <- as.numeric(issp_2007$FI_INC)
table(qcut2(issp_2007$FI_INC, 5), exclude = NULL)
issp_2007$FI_INC_rank <- qcut2(issp_2007$FI_INC, 5)
table(issp_2007$FI_INC_rank, exclude = NULL)

table(issp_2007$IE_INC, exclude = NULL)
str(issp_2007$IE_INC)
issp_2007$IE_INC <- as.numeric(issp_2007$IE_INC)
table(qcut2(issp_2007$IE_INC, 5), exclude = NULL)
issp_2007$IE_INC_rank <- qcut2(issp_2007$IE_INC, 5)
table(issp_2007$IE_INC_rank, exclude = NULL)

table(issp_2007$KR_INC, exclude = NULL)
str(issp_2007$KR_INC)
issp_2007$KR_INC <- as.numeric(issp_2007$KR_INC)
table(qcut2(issp_2007$KR_INC, 5), exclude = NULL)
issp_2007$KR_INC_rank <- qcut2(issp_2007$KR_INC, 5)
table(issp_2007$KR_INC_rank, exclude = NULL)

table(issp_2007$MX_INC, exclude = NULL)
str(issp_2007$MX_INC)
issp_2007$MX_INC <- as.numeric(issp_2007$MX_INC)
table(qcut2(issp_2007$MX_INC, 5), exclude = NULL)
issp_2007$MX_INC_rank <- qcut2(issp_2007$MX_INC, 5)
table(issp_2007$MX_INC_rank, exclude = NULL)

table(issp_2007$PH_INC, exclude = NULL)
str(issp_2007$PH_INC)
issp_2007$PH_INC <- as.numeric(issp_2007$PH_INC)
table(qcut2(issp_2007$PH_INC, 5), exclude = NULL)
issp_2007$PH_INC_rank <- qcut2(issp_2007$PH_INC, 5)
table(issp_2007$PH_INC_rank, exclude = NULL)

table(issp_2007$RU_INC, exclude = NULL)
str(issp_2007$RU_INC)
issp_2007$RU_INC <- as.numeric(issp_2007$RU_INC)
table(qcut2(issp_2007$RU_INC, 5), exclude = NULL)
issp_2007$RU_INC_rank <- qcut2(issp_2007$RU_INC, 5)
table(issp_2007$RU_INC_rank, exclude = NULL)

table(issp_2007$NZ_INC, exclude = NULL)
str(issp_2007$NZ_INC)
issp_2007$NZ_INC <- as.numeric(issp_2007$NZ_INC)
table(qcut2(issp_2007$NZ_INC, 5), exclude = NULL)
issp_2007$NZ_INC_rank <- qcut2(issp_2007$NZ_INC, 5)
table(issp_2007$NZ_INC_rank, exclude = NULL)

issp_2007$income <- rowSums(issp_2007[,c("FI_INC_rank", 
                                         "IE_INC_rank",
                                         "KR_INC_rank",
                                         "MX_INC_rank",
                                         "NL_INC_rank",
                                         "NZ_INC_rank",
                                         "PH_INC_rank",
                                         "RU_INC_rank",
                                         "GR_INC_rank")], na.rm = T)
table(issp_2007$income, exclude = NULL)
table(issp_2007$income, issp_2007$FI_INC_rank, exclude = NULL)
table(issp_2007$income, issp_2007$IE_INC_rank, exclude = NULL)
table(issp_2007$income, issp_2007$KR_INC_rank, exclude = NULL)
table(issp_2007$income, issp_2007$MX_INC_rank, exclude = NULL)
table(issp_2007$income, issp_2007$NL_INC_rank, exclude = NULL)
table(issp_2007$income, issp_2007$NZ_INC_rank, exclude = NULL)
table(issp_2007$income, issp_2007$PH_INC_rank, exclude = NULL)
table(issp_2007$income, issp_2007$RU_INC_rank, exclude = NULL)
table(issp_2007$income, issp_2007$GR_INC_rank, exclude = NULL)
issp_2007$income <- ifelse(issp_2007$income==0, NA, issp_2007$income)
table(issp_2007$income, exclude = NULL)

table(issp_2007$V5, exclude = NULL)
issp_2007$country <- NA
issp_2007$country[issp_2007$V5==246] <- 'Finland'
issp_2007$country[issp_2007$V5==2020] <- 'Greece'
issp_2007$country[issp_2007$V5==372] <- 'Ireland'
issp_2007$country[issp_2007$V5==410] <- 'South Korea'
issp_2007$country[issp_2007$V5==484] <- 'Mexico'
issp_2007$country[issp_2007$V5==528] <- 'the Netherlands'
issp_2007$country[issp_2007$V5==554] <- 'New Zealand'
issp_2007$country[issp_2007$V5==608] <- 'the Philippines'
issp_2007$country[issp_2007$V5==643] <- 'Russia'
table(issp_2007$country, exclude = NULL)
issp_2007$country <- factor(issp_2007$country, 
                            levels = c('Finland','Greece', 'Ireland', 'Mexico', 'the Netherlands', 'New Zealand', 'the Philippines', 'Russia', 'South Korea'))
table(issp_2007$country, exclude = NULL)


issp_2007 <- subset(issp_2007, subset = country == 'Finland' | 
                      country == 'Greece' |
                      country == 'Ireland' | 
                      country == 'South Korea' | 
                      country == 'Mexico' | 
                      country == 'the Netherlands' | 
                      country == 'New Zealand' | 
                      country == 'the Philippines' | country == 'Russia')

issp_2007$local_election <- NA
issp_2007$local_election[issp_2007$country=="the Netherlands" | issp_2007$country=="South Korea"] <- 1
issp_2007$local_election <- ifelse(is.na(issp_2007$local_election), 0, issp_2007$local_election)
table(issp_2007$local_election, exclude = NULL)

issp_2007$polyarchy <- NA
issp_2007$polyarchy[issp_2007$country=="Finland"] <- .887
issp_2007$polyarchy[issp_2007$country=="Greece"] <- .862
issp_2007$polyarchy[issp_2007$country=="Ireland"] <- .888
issp_2007$polyarchy[issp_2007$country=="Mexico"] <- .672
issp_2007$polyarchy[issp_2007$country=="the Netherlands"] <- .864
issp_2007$polyarchy[issp_2007$country=="New Zealand"] <- .887
issp_2007$polyarchy[issp_2007$country=="the Philippines"] <- .498
issp_2007$polyarchy[issp_2007$country=="Russia"] <- .324
issp_2007$polyarchy[issp_2007$country=="South Korea"] <- .85
table(issp_2007$polyarchy, exclude = NULL)

#### Getting Country-Region Level Variables ####
table(issp_2007$IE_REG, exclude = NULL)
table(issp_2007$FI_REG, exclude = NULL)
table(issp_2007$KR_REG, exclude = NULL)
table(issp_2007$MX_REG, exclude = NULL)
table(issp_2007$NL_REG, exclude = NULL)
table(issp_2007$NZ_REG, exclude = NULL)
table(issp_2007$PH_REG, exclude = NULL)
table(issp_2007$RU_REG, exclude = NULL)

issp_2007$region_id <- NA
issp_2007$region_id <- rowSums( issp_2007[,c("IE_REG", "FI_REG", "KR_REG",
                                             "MX_REG", "NL_REG", "NZ_REG",
                                             "PH_REG", "RU_REG")], na.rm = T)

table(issp_2007$region_id, exclude = NULL)

issp_2007$region_id <- ifelse(issp_2007$region_id==0, NA, issp_2007$region_id)

issp_2007$region_id_merging <- paste(issp_2007$V5, issp_2007$region_id)
table(issp_2007$region_id_merging, exclude = NULL)  

geocodes_temperatures <- read_excel("Geographic Dataset/geocodes_temperatures.xlsx")
geocodes_temperatures$region_id_merging <- paste(geocodes_temperatures$V5, geocodes_temperatures$region_id)
table(geocodes_temperatures$region_id_merging, exclude = NULL) 

issp_2007 <- merge(issp_2007,geocodes_temperatures,by="region_id_merging", all = T)

#### Pooled Data - Grand mean centering ####

issp_2007$PARTY_LR_avg <- mean(issp_2007$PARTY_LR, na.rm = T)
issp_2007$PARTY_LR_sd <- sd(issp_2007$PARTY_LR, na.rm = T)
issp_2007$PARTY_LR_2sd <- issp_2007$PARTY_LR_sd*2
issp_2007$PARTY_LR_beta <- (issp_2007$PARTY_LR-issp_2007$PARTY_LR_avg)/issp_2007$PARTY_LR_2sd

issp_2007$religiosity_avg <- mean(issp_2007$religiosity, na.rm = T)
issp_2007$religiosity_sd <- sd(issp_2007$religiosity, na.rm = T)
issp_2007$religiosity_2sd <- issp_2007$religiosity_sd*2
issp_2007$religiosity_beta <- (issp_2007$religiosity-issp_2007$religiosity_avg)/issp_2007$religiosity_2sd

issp_2007 <- issp_2007 %>%
  group_by(country) %>%
  mutate(group_mean_chronotype = mean(chronotype_interval, na.rm = T),
         group_sd_chronotype = sd(chronotype_interval, na.rm = T))

issp_2007$chronotype_interval_avg <- issp_2007$group_mean_chronotype
issp_2007$chronotype_interval_sd <- issp_2007$group_sd_chronotype
issp_2007$chronotype_interval_2sd <- issp_2007$chronotype_interval_sd*2
issp_2007$chronotype_interval_beta <- (issp_2007$chronotype_interval-issp_2007$chronotype_interval_avg)/issp_2007$chronotype_interval_2sd

issp_2007 <- issp_2007 %>%
  group_by(country) %>%
  mutate(group_mean_sleep_duration = mean(sleep_duration, na.rm = T),
         group_sd_sleep_duration = sd(sleep_duration, na.rm = T))

issp_2007$sleep_duration_avg <- issp_2007$group_mean_sleep_duration
issp_2007$sleep_duration_sd <- issp_2007$group_sd_sleep_duration
issp_2007$sleep_duration_2sd <- issp_2007$sleep_duration_sd*2
issp_2007$sleep_duration_beta <- (issp_2007$sleep_duration-issp_2007$sleep_duration_avg)/issp_2007$sleep_duration_2sd

table(issp_2007$urban_rural_area, exclude = NULL)
issp_2007$urban_rural_area_avg <- mean(issp_2007$urban_rural_area, na.rm = T)
issp_2007$urban_rural_area_sd <- sd(issp_2007$urban_rural_area, na.rm = T)
issp_2007$urban_rural_area_2sd <- issp_2007$urban_rural_area_sd*2
issp_2007$urban_rural_area_beta <- (issp_2007$urban_rural_area-issp_2007$urban_rural_area_avg)/issp_2007$urban_rural_area_2sd

table(issp_2007$age, exclude = NULL)
issp_2007$age_avg <- mean(issp_2007$age, na.rm = T)
issp_2007$age_sd <- sd(issp_2007$age, na.rm = T)
issp_2007$age_2sd <- issp_2007$age_sd*2
issp_2007$age_beta <- (issp_2007$age-issp_2007$age_avg)/issp_2007$age_2sd

table(issp_2007$degree, exclude = NULL)
issp_2007$degree_avg <- mean(issp_2007$degree, na.rm = T)
issp_2007$degree_sd <- sd(issp_2007$degree, na.rm = T)
issp_2007$degree_2sd <- issp_2007$degree_sd*2
issp_2007$degree_beta <- (issp_2007$degree-issp_2007$degree_avg)/issp_2007$degree_2sd

issp_2007$sex_dummy_avg <- mean(issp_2007$sex, na.rm = T)
issp_2007$sex_beta <- (issp_2007$sex-issp_2007$sex_dummy_avg)

issp_2007$political_interest_avg <- mean(issp_2007$political_interest, na.rm = T)
issp_2007$political_interest_sd <- sd(issp_2007$political_interest, na.rm = T)
issp_2007$political_interest_2sd <- issp_2007$political_interest_sd*2
issp_2007$political_interest_beta <- (issp_2007$political_interest-issp_2007$political_interest_avg)/issp_2007$political_interest_2sd

issp_2007$income_avg <- mean(issp_2007$income, na.rm = T)
issp_2007$income_sd <- sd(issp_2007$income, na.rm = T)
issp_2007$income_2sd <- issp_2007$income_sd*2
issp_2007$income_beta <- (issp_2007$income-issp_2007$income_avg)/issp_2007$income_2sd

issp_2007$turnout_dummy_avg <- mean(issp_2007$turnout, na.rm = T)
issp_2007$turnout_beta <- (issp_2007$turnout-issp_2007$turnout_dummy_avg)

issp_2007$polyarchy_avg <- mean(issp_2007$polyarchy, na.rm = T)
issp_2007$polyarchy_sd <- sd(issp_2007$polyarchy, na.rm = T)
issp_2007$polyarchy_2sd <- issp_2007$polyarchy_sd*2
issp_2007$polyarchy_beta <- (issp_2007$polyarchy-issp_2007$polyarchy_avg)/issp_2007$polyarchy_2sd

table(issp_2007$country, issp_2007$mid_field_season, exclude = NULL)
issp_2007$mid_field_season <- ifelse(is.na(issp_2007$mid_field_season) & issp_2007$country=="New Zealand",
                                     "Spring", issp_2007$mid_field_season)
issp_2007$mid_field_season <- ifelse(is.na(issp_2007$mid_field_season) & issp_2007$country=="Greece",
                                     "Winter", issp_2007$mid_field_season)

table(issp_2007$country, exclude = NULL)
issp_2007$election_season <- NA
issp_2007$election_season[issp_2007$country=="Finland" |
                            issp_2007$country=="Ireland" |
                            issp_2007$country=="the Netherlands" |
                            issp_2007$country=="New Zealand" |
                            issp_2007$country=="the Philippines" |
                            issp_2007$country=="Russia" |
                            issp_2007$country=="South Korea"] <- "Spring"
issp_2007$election_season[issp_2007$country=="Greece" |
                            issp_2007$country=="Mexico"] <- "Summer"
table(issp_2007$election_season, exclude = NULL)
issp_2007 <- fastDummies::dummy_cols(issp_2007, select_columns = 'election_season')

table(issp_2007$election_season_Summer, exclude = NULL)
issp_2007$election_season_Summer_avg <- mean(issp_2007$election_season_Summer, na.rm = T)
issp_2007$election_season_Summer_beta <- (issp_2007$election_season_Summer-issp_2007$election_season_Summer_avg)
table(issp_2007$election_season_Summer_beta, exclude = NULL)

table(issp_2007$latitude, exclude = NULL)
issp_2007$latitude_avg <- mean(issp_2007$latitude, na.rm = T)
issp_2007$latitude_sd <- sd(issp_2007$latitude, na.rm = T)
issp_2007$latitude_2sd <- issp_2007$latitude_sd*2
issp_2007$latitude_beta <- (issp_2007$latitude-issp_2007$latitude_avg)/issp_2007$latitude_2sd

table(issp_2007$longitude, exclude = NULL)
issp_2007$longitude_avg <- mean(issp_2007$longitude, na.rm = T)
issp_2007$longitude_sd <- sd(issp_2007$longitude, na.rm = T)
issp_2007$longitude_2sd <- issp_2007$longitude_sd*2
issp_2007$longitude_beta <- (issp_2007$longitude-issp_2007$longitude_avg)/issp_2007$longitude_2sd

table(issp_2007$mean_temperature, exclude = NULL)
issp_2007$mean_temperature_avg <- mean(issp_2007$mean_temperature, na.rm = T)
issp_2007$mean_temperature_sd <- sd(issp_2007$mean_temperature, na.rm = T)
issp_2007$mean_temperature_2sd <- issp_2007$mean_temperature_sd*2
issp_2007$mean_temperature_beta <- (issp_2007$mean_temperature-issp_2007$mean_temperature_avg)/issp_2007$mean_temperature_2sd

table(issp_2007$country, exclude = NULL)
issp_2007$time_duration_election_turnout <- NA
issp_2007$time_duration_election_turnout[issp_2007$country=="Finland"] <- 0
issp_2007$time_duration_election_turnout[issp_2007$country=="Greece"] <- 1
issp_2007$time_duration_election_turnout[issp_2007$country=="Ireland"] <- 1
issp_2007$time_duration_election_turnout[issp_2007$country=="Mexico"] <- 2
issp_2007$time_duration_election_turnout[issp_2007$country=="the Netherlands"] <- 2
issp_2007$time_duration_election_turnout[issp_2007$country=="New Zealand"] <- 2
issp_2007$time_duration_election_turnout[issp_2007$country=="the Philippines"] <- 4
issp_2007$time_duration_election_turnout[issp_2007$country=="Russia"] <- 3
issp_2007$time_duration_election_turnout[issp_2007$country=="South Korea"] <- 1
table(issp_2007$time_duration_election_turnout, exclude = NULL)

issp_2007$time_duration_election_turnout_avg <- mean(issp_2007$time_duration_election_turnout, na.rm = T)
issp_2007$time_duration_election_turnout_sd <- sd(issp_2007$time_duration_election_turnout, na.rm = T)
issp_2007$time_duration_election_turnout_2sd <- issp_2007$time_duration_election_turnout_sd*2
issp_2007$time_duration_election_turnout_beta <- (issp_2007$time_duration_election_turnout-issp_2007$time_duration_election_turnout_avg)/issp_2007$time_duration_election_turnout_2sd
table(issp_2007$time_duration_election_turnout_beta, exclude = NULL)

#### Manuscript, Table 1 Descriptive statistics. ####

table1::table1(~  turnout + 
                 sleep_duration +  
                 chronotype_interval +  
                 urban_rural_area + 
                 age +  
                 degree +  
                 income +  
                 sex +  
                 political_interest + 
                 PARTY_LR +  
                 religiosity| country, data=issp_2007, digits=2)


#### Main Models ####

issp_2007_turnout_sleep_duration_model1 <- glmer(turnout ~ sleep_duration_beta + chronotype_interval_beta + 
                                                   (1 + sleep_duration_beta + chronotype_interval_beta | country/local_election), 
                                                 data =  issp_2007, family = "binomial", nAGQ=0)
summary(issp_2007_turnout_sleep_duration_model1)
tab_model(issp_2007_turnout_sleep_duration_model1, show.ci = 0.95)

issp_2007_turnout_sleep_duration_model2 <- glmer(turnout ~ sleep_duration_beta + chronotype_interval_beta + 
                                                   urban_rural_area_beta + (1  + sleep_duration_beta + chronotype_interval_beta | country/local_election), 
                                                 data =  issp_2007, family = "binomial", nAGQ=0)
summary(issp_2007_turnout_sleep_duration_model2)
tab_model(issp_2007_turnout_sleep_duration_model2, show.ci = 0.95)

issp_2007_turnout_sleep_duration_model3 <- glmer(turnout ~ sleep_duration_beta + chronotype_interval_beta + 
                                                   urban_rural_area_beta +
                                                   age_beta + degree_beta + income_beta + sex_beta + political_interest_beta + PARTY_LR_beta + 
                                                   (1  + sleep_duration_beta + chronotype_interval_beta | country/local_election), 
                                                 data =  issp_2007, family = "binomial", nAGQ=0)
summary(issp_2007_turnout_sleep_duration_model3)
tab_model(issp_2007_turnout_sleep_duration_model3, show.ci = 0.95)

issp_2007_turnout_sleep_duration_model4 <- glmer(turnout ~ sleep_duration_beta + chronotype_interval_beta + 
                                                   urban_rural_area_beta +
                                                   age_beta + degree_beta + income_beta + sex_beta + political_interest_beta + religiosity_beta + 
                                                   (1  + sleep_duration_beta + chronotype_interval_beta | country/local_election), 
                                                 data =  issp_2007, family = "binomial", nAGQ=0)
summary(issp_2007_turnout_sleep_duration_model4)
tab_model(issp_2007_turnout_sleep_duration_model4, show.ci = 0.95)

####  Online Appendices, Table 1 Multilevel logistic regression models of turnout by sleep duration and chronotype. ####

stargazer(issp_2007_turnout_sleep_duration_model1, issp_2007_turnout_sleep_duration_model2, issp_2007_turnout_sleep_duration_model3, issp_2007_turnout_sleep_duration_model4,
          type = "html", title=" ", digits=2, out="Tables/models_issp_2007_turnout_sleep_duration_chronotype.htm",
          model.numbers = F,
          column.labels = c("Model 1", "Model 2", "Model 3", "Model 4"),
          covariate.labels = c("Sleep Duration", 
                               "Chronotype",
                               "Urban-Rural Area of Residence", 
                               "Age", 
                               "Education", 
                               "Income", 
                               "Sex: Male (Base: Female)",
                               "Level on Interest in Politics", 
                               "Left-Right Ideological Placement", 
                               "Religious Attendance", 
                               "Intercept"))

####  Online Appendices, Table 2 Predicted effects of sleep duration and chronotype on turnout for each country. ####

coef(issp_2007_turnout_sleep_duration_model1)
arm::se.coef(issp_2007_turnout_sleep_duration_model1)

coef(issp_2007_turnout_sleep_duration_model1)$country
coef_issp_2007_turnout_sleep_duration_model1 <- data.frame(coef(issp_2007_turnout_sleep_duration_model1)$country)
coef_issp_2007_turnout_sleep_duration_model1 <- subset(coef_issp_2007_turnout_sleep_duration_model1, select = -c(X.Intercept.))
coef_issp_2007_turnout_sleep_duration_model1$country <- row.names(coef_issp_2007_turnout_sleep_duration_model1)
names(coef_issp_2007_turnout_sleep_duration_model1)[names(coef_issp_2007_turnout_sleep_duration_model1)=="sleep_duration_beta"] <- "coef_sleep_duration_beta"
names(coef_issp_2007_turnout_sleep_duration_model1)[names(coef_issp_2007_turnout_sleep_duration_model1)=="chronotype_interval_beta"] <- "coef_chronotype_interval_beta"


se_coef_issp_2007_turnout_sleep_duration_model1 <- data.frame(arm::se.coef(issp_2007_turnout_sleep_duration_model1)$country)
se_coef_issp_2007_turnout_sleep_duration_model1 <- subset(se_coef_issp_2007_turnout_sleep_duration_model1, select = -c(X.Intercept.))
se_coef_issp_2007_turnout_sleep_duration_model1$country <- row.names(se_coef_issp_2007_turnout_sleep_duration_model1)
names(se_coef_issp_2007_turnout_sleep_duration_model1)[names(se_coef_issp_2007_turnout_sleep_duration_model1)=="sleep_duration_beta"] <- "se_sleep_duration_beta"
names(se_coef_issp_2007_turnout_sleep_duration_model1)[names(se_coef_issp_2007_turnout_sleep_duration_model1)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

coef_se_issp_2007_turnout_sleep_duration_model1 <- merge(coef_issp_2007_turnout_sleep_duration_model1, se_coef_issp_2007_turnout_sleep_duration_model1, by="country")

coef_se_issp_2007_turnout_sleep_duration_model1$slp.dur.cl.90 <- coef_se_issp_2007_turnout_sleep_duration_model1$coef_sleep_duration_beta - 1.645*coef_se_issp_2007_turnout_sleep_duration_model1$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model1$slp.dur.cu.90 <- coef_se_issp_2007_turnout_sleep_duration_model1$coef_sleep_duration_beta + 1.645*coef_se_issp_2007_turnout_sleep_duration_model1$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model1$slp.dur.cl.95 <- coef_se_issp_2007_turnout_sleep_duration_model1$coef_sleep_duration_beta - 1.96*coef_se_issp_2007_turnout_sleep_duration_model1$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model1$slp.dur.cu.95 <- coef_se_issp_2007_turnout_sleep_duration_model1$coef_sleep_duration_beta + 1.96*coef_se_issp_2007_turnout_sleep_duration_model1$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model1$slp.dur.cl.99 <- coef_se_issp_2007_turnout_sleep_duration_model1$coef_sleep_duration_beta - 2.576*coef_se_issp_2007_turnout_sleep_duration_model1$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model1$slp.dur.cu.99 <- coef_se_issp_2007_turnout_sleep_duration_model1$coef_sleep_duration_beta + 2.576*coef_se_issp_2007_turnout_sleep_duration_model1$se_sleep_duration_beta

coef_se_issp_2007_turnout_sleep_duration_model1$chro.cl.90 <- coef_se_issp_2007_turnout_sleep_duration_model1$coef_chronotype_interval_beta - 1.645*coef_se_issp_2007_turnout_sleep_duration_model1$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model1$chro.cu.90 <- coef_se_issp_2007_turnout_sleep_duration_model1$coef_chronotype_interval_beta + 1.645*coef_se_issp_2007_turnout_sleep_duration_model1$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model1$chro.cl.95 <- coef_se_issp_2007_turnout_sleep_duration_model1$coef_chronotype_interval_beta - 1.96*coef_se_issp_2007_turnout_sleep_duration_model1$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model1$chro.cu.95 <- coef_se_issp_2007_turnout_sleep_duration_model1$coef_chronotype_interval_beta + 1.96*coef_se_issp_2007_turnout_sleep_duration_model1$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model1$chro.cl.99 <- coef_se_issp_2007_turnout_sleep_duration_model1$coef_chronotype_interval_beta - 2.576*coef_se_issp_2007_turnout_sleep_duration_model1$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model1$chro.cu.99 <- coef_se_issp_2007_turnout_sleep_duration_model1$coef_chronotype_interval_beta + 2.576*coef_se_issp_2007_turnout_sleep_duration_model1$se_chronotype_interval_beta

coef_se_issp_2007_turnout_sleep_duration_model1$coef_sleep_duration_beta <- round(coef_se_issp_2007_turnout_sleep_duration_model1$coef_sleep_duration_beta, 2)
coef_se_issp_2007_turnout_sleep_duration_model1$se_sleep_duration_beta <- round(coef_se_issp_2007_turnout_sleep_duration_model1$se_sleep_duration_beta, 2)
coef_se_issp_2007_turnout_sleep_duration_model1$coef_chronotype_interval_beta <- round(coef_se_issp_2007_turnout_sleep_duration_model1$coef_chronotype_interval_beta, 2)
coef_se_issp_2007_turnout_sleep_duration_model1$se_chronotype_interval_beta <- round(coef_se_issp_2007_turnout_sleep_duration_model1$se_chronotype_interval_beta, 2)
View(coef_se_issp_2007_turnout_sleep_duration_model1)

coef(issp_2007_turnout_sleep_duration_model2)
arm::se.coef(issp_2007_turnout_sleep_duration_model2)

coef(issp_2007_turnout_sleep_duration_model2)$country
coef_issp_2007_turnout_sleep_duration_model2 <- data.frame(coef(issp_2007_turnout_sleep_duration_model2)$country)
coef_issp_2007_turnout_sleep_duration_model2 <- subset(coef_issp_2007_turnout_sleep_duration_model2, select = c("sleep_duration_beta", "chronotype_interval_beta"))
coef_issp_2007_turnout_sleep_duration_model2$country <- row.names(coef_issp_2007_turnout_sleep_duration_model2)
names(coef_issp_2007_turnout_sleep_duration_model2)[names(coef_issp_2007_turnout_sleep_duration_model2)=="sleep_duration_beta"] <- "coef_sleep_duration_beta"
names(coef_issp_2007_turnout_sleep_duration_model2)[names(coef_issp_2007_turnout_sleep_duration_model2)=="chronotype_interval_beta"] <- "coef_chronotype_interval_beta"


se_coef_issp_2007_turnout_sleep_duration_model2 <- data.frame(arm::se.coef(issp_2007_turnout_sleep_duration_model2)$country)
se_coef_issp_2007_turnout_sleep_duration_model2 <- subset(se_coef_issp_2007_turnout_sleep_duration_model2, select = c("sleep_duration_beta", "chronotype_interval_beta"))
se_coef_issp_2007_turnout_sleep_duration_model2$country <- row.names(se_coef_issp_2007_turnout_sleep_duration_model2)
names(se_coef_issp_2007_turnout_sleep_duration_model2)[names(se_coef_issp_2007_turnout_sleep_duration_model2)=="sleep_duration_beta"] <- "se_sleep_duration_beta"
names(se_coef_issp_2007_turnout_sleep_duration_model2)[names(se_coef_issp_2007_turnout_sleep_duration_model2)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

coef_se_issp_2007_turnout_sleep_duration_model2 <- merge(coef_issp_2007_turnout_sleep_duration_model2, se_coef_issp_2007_turnout_sleep_duration_model2, by="country")

coef_se_issp_2007_turnout_sleep_duration_model2$slp.dur.cl.90 <- coef_se_issp_2007_turnout_sleep_duration_model2$coef_sleep_duration_beta - 1.645*coef_se_issp_2007_turnout_sleep_duration_model2$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model2$slp.dur.cu.90 <- coef_se_issp_2007_turnout_sleep_duration_model2$coef_sleep_duration_beta + 1.645*coef_se_issp_2007_turnout_sleep_duration_model2$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model2$slp.dur.cl.95 <- coef_se_issp_2007_turnout_sleep_duration_model2$coef_sleep_duration_beta - 1.96*coef_se_issp_2007_turnout_sleep_duration_model2$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model2$slp.dur.cu.95 <- coef_se_issp_2007_turnout_sleep_duration_model2$coef_sleep_duration_beta + 1.96*coef_se_issp_2007_turnout_sleep_duration_model2$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model2$slp.dur.cl.99 <- coef_se_issp_2007_turnout_sleep_duration_model2$coef_sleep_duration_beta - 2.576*coef_se_issp_2007_turnout_sleep_duration_model2$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model2$slp.dur.cu.99 <- coef_se_issp_2007_turnout_sleep_duration_model2$coef_sleep_duration_beta + 2.576*coef_se_issp_2007_turnout_sleep_duration_model2$se_sleep_duration_beta

coef_se_issp_2007_turnout_sleep_duration_model2$chro.cl.90 <- coef_se_issp_2007_turnout_sleep_duration_model2$coef_chronotype_interval_beta - 1.645*coef_se_issp_2007_turnout_sleep_duration_model2$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model2$chro.cu.90 <- coef_se_issp_2007_turnout_sleep_duration_model2$coef_chronotype_interval_beta + 1.645*coef_se_issp_2007_turnout_sleep_duration_model2$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model2$chro.cl.95 <- coef_se_issp_2007_turnout_sleep_duration_model2$coef_chronotype_interval_beta - 1.96*coef_se_issp_2007_turnout_sleep_duration_model2$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model2$chro.cu.95 <- coef_se_issp_2007_turnout_sleep_duration_model2$coef_chronotype_interval_beta + 1.96*coef_se_issp_2007_turnout_sleep_duration_model2$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model2$chro.cl.99 <- coef_se_issp_2007_turnout_sleep_duration_model2$coef_chronotype_interval_beta - 2.576*coef_se_issp_2007_turnout_sleep_duration_model2$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model2$chro.cu.99 <- coef_se_issp_2007_turnout_sleep_duration_model2$coef_chronotype_interval_beta + 2.576*coef_se_issp_2007_turnout_sleep_duration_model2$se_chronotype_interval_beta

coef_se_issp_2007_turnout_sleep_duration_model2$coef_sleep_duration_beta <- round(coef_se_issp_2007_turnout_sleep_duration_model2$coef_sleep_duration_beta, 2)
coef_se_issp_2007_turnout_sleep_duration_model2$se_sleep_duration_beta <- round(coef_se_issp_2007_turnout_sleep_duration_model2$se_sleep_duration_beta, 2)
coef_se_issp_2007_turnout_sleep_duration_model2$coef_chronotype_interval_beta <- round(coef_se_issp_2007_turnout_sleep_duration_model2$coef_chronotype_interval_beta, 2)
coef_se_issp_2007_turnout_sleep_duration_model2$se_chronotype_interval_beta <- round(coef_se_issp_2007_turnout_sleep_duration_model2$se_chronotype_interval_beta, 2)
View(coef_se_issp_2007_turnout_sleep_duration_model2)

coef(issp_2007_turnout_sleep_duration_model3)
arm::se.coef(issp_2007_turnout_sleep_duration_model3)

coef(issp_2007_turnout_sleep_duration_model3)$country
coef_issp_2007_turnout_sleep_duration_model3 <- data.frame(coef(issp_2007_turnout_sleep_duration_model3)$country)
coef_issp_2007_turnout_sleep_duration_model3 <- subset(coef_issp_2007_turnout_sleep_duration_model3, select = c("sleep_duration_beta", "chronotype_interval_beta"))
coef_issp_2007_turnout_sleep_duration_model3$country <- row.names(coef_issp_2007_turnout_sleep_duration_model3)
names(coef_issp_2007_turnout_sleep_duration_model3)[names(coef_issp_2007_turnout_sleep_duration_model3)=="sleep_duration_beta"] <- "coef_sleep_duration_beta"
names(coef_issp_2007_turnout_sleep_duration_model3)[names(coef_issp_2007_turnout_sleep_duration_model3)=="chronotype_interval_beta"] <- "coef_chronotype_interval_beta"


se_coef_issp_2007_turnout_sleep_duration_model3 <- data.frame(arm::se.coef(issp_2007_turnout_sleep_duration_model3)$country)
se_coef_issp_2007_turnout_sleep_duration_model3 <- subset(se_coef_issp_2007_turnout_sleep_duration_model3, select = c("sleep_duration_beta", "chronotype_interval_beta"))
se_coef_issp_2007_turnout_sleep_duration_model3$country <- row.names(se_coef_issp_2007_turnout_sleep_duration_model3)
names(se_coef_issp_2007_turnout_sleep_duration_model3)[names(se_coef_issp_2007_turnout_sleep_duration_model3)=="sleep_duration_beta"] <- "se_sleep_duration_beta"
names(se_coef_issp_2007_turnout_sleep_duration_model3)[names(se_coef_issp_2007_turnout_sleep_duration_model3)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

coef_se_issp_2007_turnout_sleep_duration_model3 <- merge(coef_issp_2007_turnout_sleep_duration_model3, se_coef_issp_2007_turnout_sleep_duration_model3, by="country")

coef_se_issp_2007_turnout_sleep_duration_model3$slp.dur.cl.90 <- coef_se_issp_2007_turnout_sleep_duration_model3$coef_sleep_duration_beta - 1.645*coef_se_issp_2007_turnout_sleep_duration_model3$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model3$slp.dur.cu.90 <- coef_se_issp_2007_turnout_sleep_duration_model3$coef_sleep_duration_beta + 1.645*coef_se_issp_2007_turnout_sleep_duration_model3$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model3$slp.dur.cl.95 <- coef_se_issp_2007_turnout_sleep_duration_model3$coef_sleep_duration_beta - 1.96*coef_se_issp_2007_turnout_sleep_duration_model3$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model3$slp.dur.cu.95 <- coef_se_issp_2007_turnout_sleep_duration_model3$coef_sleep_duration_beta + 1.96*coef_se_issp_2007_turnout_sleep_duration_model3$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model3$slp.dur.cl.99 <- coef_se_issp_2007_turnout_sleep_duration_model3$coef_sleep_duration_beta - 2.576*coef_se_issp_2007_turnout_sleep_duration_model3$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model3$slp.dur.cu.99 <- coef_se_issp_2007_turnout_sleep_duration_model3$coef_sleep_duration_beta + 2.576*coef_se_issp_2007_turnout_sleep_duration_model3$se_sleep_duration_beta

coef_se_issp_2007_turnout_sleep_duration_model3$chro.cl.90 <- coef_se_issp_2007_turnout_sleep_duration_model3$coef_chronotype_interval_beta - 1.645*coef_se_issp_2007_turnout_sleep_duration_model3$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model3$chro.cu.90 <- coef_se_issp_2007_turnout_sleep_duration_model3$coef_chronotype_interval_beta + 1.645*coef_se_issp_2007_turnout_sleep_duration_model3$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model3$chro.cl.95 <- coef_se_issp_2007_turnout_sleep_duration_model3$coef_chronotype_interval_beta - 1.96*coef_se_issp_2007_turnout_sleep_duration_model3$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model3$chro.cu.95 <- coef_se_issp_2007_turnout_sleep_duration_model3$coef_chronotype_interval_beta + 1.96*coef_se_issp_2007_turnout_sleep_duration_model3$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model3$chro.cl.99 <- coef_se_issp_2007_turnout_sleep_duration_model3$coef_chronotype_interval_beta - 2.576*coef_se_issp_2007_turnout_sleep_duration_model3$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model3$chro.cu.99 <- coef_se_issp_2007_turnout_sleep_duration_model3$coef_chronotype_interval_beta + 2.576*coef_se_issp_2007_turnout_sleep_duration_model3$se_chronotype_interval_beta

coef_se_issp_2007_turnout_sleep_duration_model3$coef_sleep_duration_beta <- round(coef_se_issp_2007_turnout_sleep_duration_model3$coef_sleep_duration_beta, 2)
coef_se_issp_2007_turnout_sleep_duration_model3$se_sleep_duration_beta <- round(coef_se_issp_2007_turnout_sleep_duration_model3$se_sleep_duration_beta, 2)
coef_se_issp_2007_turnout_sleep_duration_model3$coef_chronotype_interval_beta <- round(coef_se_issp_2007_turnout_sleep_duration_model3$coef_chronotype_interval_beta, 2)
coef_se_issp_2007_turnout_sleep_duration_model3$se_chronotype_interval_beta <- round(coef_se_issp_2007_turnout_sleep_duration_model3$se_chronotype_interval_beta, 2)
View(coef_se_issp_2007_turnout_sleep_duration_model3)

coef(issp_2007_turnout_sleep_duration_model4)
arm::se.coef(issp_2007_turnout_sleep_duration_model4)

coef(issp_2007_turnout_sleep_duration_model4)$country
coef_issp_2007_turnout_sleep_duration_model4 <- data.frame(coef(issp_2007_turnout_sleep_duration_model4)$country)
coef_issp_2007_turnout_sleep_duration_model4 <- subset(coef_issp_2007_turnout_sleep_duration_model4, select = c("sleep_duration_beta", "chronotype_interval_beta"))
coef_issp_2007_turnout_sleep_duration_model4$country <- row.names(coef_issp_2007_turnout_sleep_duration_model4)
names(coef_issp_2007_turnout_sleep_duration_model4)[names(coef_issp_2007_turnout_sleep_duration_model4)=="sleep_duration_beta"] <- "coef_sleep_duration_beta"
names(coef_issp_2007_turnout_sleep_duration_model4)[names(coef_issp_2007_turnout_sleep_duration_model4)=="chronotype_interval_beta"] <- "coef_chronotype_interval_beta"


se_coef_issp_2007_turnout_sleep_duration_model4 <- data.frame(arm::se.coef(issp_2007_turnout_sleep_duration_model4)$country)
se_coef_issp_2007_turnout_sleep_duration_model4 <- subset(se_coef_issp_2007_turnout_sleep_duration_model4, select = c("sleep_duration_beta", "chronotype_interval_beta"))
se_coef_issp_2007_turnout_sleep_duration_model4$country <- row.names(se_coef_issp_2007_turnout_sleep_duration_model4)
names(se_coef_issp_2007_turnout_sleep_duration_model4)[names(se_coef_issp_2007_turnout_sleep_duration_model4)=="sleep_duration_beta"] <- "se_sleep_duration_beta"
names(se_coef_issp_2007_turnout_sleep_duration_model4)[names(se_coef_issp_2007_turnout_sleep_duration_model4)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

coef_se_issp_2007_turnout_sleep_duration_model4 <- merge(coef_issp_2007_turnout_sleep_duration_model4, se_coef_issp_2007_turnout_sleep_duration_model4, by="country")

coef_se_issp_2007_turnout_sleep_duration_model4$slp.dur.cl.90 <- coef_se_issp_2007_turnout_sleep_duration_model4$coef_sleep_duration_beta - 1.645*coef_se_issp_2007_turnout_sleep_duration_model4$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model4$slp.dur.cu.90 <- coef_se_issp_2007_turnout_sleep_duration_model4$coef_sleep_duration_beta + 1.645*coef_se_issp_2007_turnout_sleep_duration_model4$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model4$slp.dur.cl.95 <- coef_se_issp_2007_turnout_sleep_duration_model4$coef_sleep_duration_beta - 1.96*coef_se_issp_2007_turnout_sleep_duration_model4$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model4$slp.dur.cu.95 <- coef_se_issp_2007_turnout_sleep_duration_model4$coef_sleep_duration_beta + 1.96*coef_se_issp_2007_turnout_sleep_duration_model4$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model4$slp.dur.cl.99 <- coef_se_issp_2007_turnout_sleep_duration_model4$coef_sleep_duration_beta - 2.576*coef_se_issp_2007_turnout_sleep_duration_model4$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model4$slp.dur.cu.99 <- coef_se_issp_2007_turnout_sleep_duration_model4$coef_sleep_duration_beta + 2.576*coef_se_issp_2007_turnout_sleep_duration_model4$se_sleep_duration_beta

coef_se_issp_2007_turnout_sleep_duration_model4$chro.cl.90 <- coef_se_issp_2007_turnout_sleep_duration_model4$coef_chronotype_interval_beta - 1.645*coef_se_issp_2007_turnout_sleep_duration_model4$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model4$chro.cu.90 <- coef_se_issp_2007_turnout_sleep_duration_model4$coef_chronotype_interval_beta + 1.645*coef_se_issp_2007_turnout_sleep_duration_model4$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model4$chro.cl.95 <- coef_se_issp_2007_turnout_sleep_duration_model4$coef_chronotype_interval_beta - 1.96*coef_se_issp_2007_turnout_sleep_duration_model4$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model4$chro.cu.95 <- coef_se_issp_2007_turnout_sleep_duration_model4$coef_chronotype_interval_beta + 1.96*coef_se_issp_2007_turnout_sleep_duration_model4$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model4$chro.cl.99 <- coef_se_issp_2007_turnout_sleep_duration_model4$coef_chronotype_interval_beta - 2.576*coef_se_issp_2007_turnout_sleep_duration_model4$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model4$chro.cu.99 <- coef_se_issp_2007_turnout_sleep_duration_model4$coef_chronotype_interval_beta + 2.576*coef_se_issp_2007_turnout_sleep_duration_model4$se_chronotype_interval_beta

coef_se_issp_2007_turnout_sleep_duration_model4$coef_sleep_duration_beta <- round(coef_se_issp_2007_turnout_sleep_duration_model4$coef_sleep_duration_beta, 2)
coef_se_issp_2007_turnout_sleep_duration_model4$se_sleep_duration_beta <- round(coef_se_issp_2007_turnout_sleep_duration_model4$se_sleep_duration_beta, 2)
coef_se_issp_2007_turnout_sleep_duration_model4$coef_chronotype_interval_beta <- round(coef_se_issp_2007_turnout_sleep_duration_model4$coef_chronotype_interval_beta, 2)
coef_se_issp_2007_turnout_sleep_duration_model4$se_chronotype_interval_beta <- round(coef_se_issp_2007_turnout_sleep_duration_model4$se_chronotype_interval_beta, 2)
View(coef_se_issp_2007_turnout_sleep_duration_model4)

#### Manuscript, Fig. 1. Predicted effects of sleep duration and chronotype on turnout.  ####

View(coef_se_issp_2007_turnout_sleep_duration_model1)
View(coef_se_issp_2007_turnout_sleep_duration_model2)
View(coef_se_issp_2007_turnout_sleep_duration_model3)
View(coef_se_issp_2007_turnout_sleep_duration_model4)

coef_se_issp_2007_turnout_sleep_duration_model1$Model <- "Model 1"
coef_se_issp_2007_turnout_sleep_duration_model2$Model <- "Model 2"
coef_se_issp_2007_turnout_sleep_duration_model3$Model <- "Model 3"
coef_se_issp_2007_turnout_sleep_duration_model4$Model <- "Model 4"

coef_plot_data <- dplyr::bind_rows(coef_se_issp_2007_turnout_sleep_duration_model1,
                                   coef_se_issp_2007_turnout_sleep_duration_model2,
                                   coef_se_issp_2007_turnout_sleep_duration_model3,
                                   coef_se_issp_2007_turnout_sleep_duration_model4)

Predicted_Values_Data <- reshape2::melt(subset(coef_plot_data, select = c("coef_sleep_duration_beta", "coef_chronotype_interval_beta", "country", "Model")), id.vars=c("country", "Model"))
names(Predicted_Values_Data)[names(Predicted_Values_Data)=="country"] <- "Country"
names(Predicted_Values_Data)[names(Predicted_Values_Data)=="variable"] <- "Variable"
names(Predicted_Values_Data)[names(Predicted_Values_Data)=="value"] <- "Effects"
Predicted_Values_Data$Variable <- ifelse(Predicted_Values_Data$Variable=="coef_sleep_duration_beta", "Sleep Duration", "Chronotype")
Predicted_Values_Data$merging_ID <- paste(Predicted_Values_Data$Country, Predicted_Values_Data$Model, Predicted_Values_Data$Variable)


SE_Values_Data <- reshape2::melt(subset(coef_plot_data, select = c("se_sleep_duration_beta", "se_chronotype_interval_beta", "country", "Model")), id.vars=c("country", "Model"))
names(SE_Values_Data)[names(SE_Values_Data)=="country"] <- "Country"
names(SE_Values_Data)[names(SE_Values_Data)=="variable"] <- "Variable"
names(SE_Values_Data)[names(SE_Values_Data)=="value"] <- "SE"
SE_Values_Data$Variable <- ifelse(SE_Values_Data$Variable=="se_sleep_duration_beta", "Sleep Duration", "Chronotype")
SE_Values_Data$merging_ID <- paste(SE_Values_Data$Country, SE_Values_Data$Model, SE_Values_Data$Variable)

coef_plot_data <- merge(Predicted_Values_Data, SE_Values_Data, by="merging_ID")
coef_plot_data <- subset(coef_plot_data, select = -c(Country.y, Model.y, Variable.y))

names(coef_plot_data)[names(coef_plot_data)=="Country.x"] <- "Country"
names(coef_plot_data)[names(coef_plot_data)=="Model.x"] <- "Model"
names(coef_plot_data)[names(coef_plot_data)=="Variable.x"] <- "Variable"
names(coef_plot_data)[names(coef_plot_data)=="Variable"] <- "Independent Variables"
names(coef_plot_data)[names(coef_plot_data)=="Independent Variables"] <- "Independent Variables:"

coef_plot_data$CI.L.95 <- coef_plot_data$Effects - 1.96*coef_plot_data$SE
coef_plot_data$CI.U.95 <- coef_plot_data$Effects + 1.96*coef_plot_data$SE

coef_plot_data$CI.L.99 <- coef_plot_data$Effects - 2.576*coef_plot_data$SE
coef_plot_data$CI.U.99 <- coef_plot_data$Effects + 2.576*coef_plot_data$SE

table(coef_plot_data$Country, exclude = NULL)
coef_plot_data$Country <- factor(coef_plot_data$Country, levels = c("Finland", "Greece", "Ireland", "Mexico",
                                                                    "the Netherlands", "New Zealand", "the Philippines", "Russia", "South Korea"))

coef_plot_data <- ggplot(coef_plot_data, aes(x=Model, y = Effects, shape=`Independent Variables:`)) +
  geom_point(position=position_dodge(width=1), size=3, colour="black")  +
  geom_errorbar(aes(ymax = CI.U.99, ymin = CI.L.99), width = 0, position=position_dodge(width=1), colour="black", size=0.5) +
  geom_errorbar(aes(ymax = CI.U.95, ymin = CI.L.95), width = 0, position=position_dodge(width=1), colour="black", size=1.5) +
  theme_classic() + scale_shape_manual(values=c(0, 1)) + facet_wrap(.~Country) +
  theme(axis.text.x=element_text(colour="black", size = 12), axis.text.y=element_text(colour="black", size = 12)) +
  theme(axis.title=element_text(size=14, colour="black", face="bold"), legend.text=element_text(size=14), legend.title=element_text(size=14)) +
  labs(    x = "\nModels", 
           y = "\n Predicted Effects (Sum of Fixed and Random Effects) on Turnout\n",
           title = "") +
  geom_hline(yintercept = 0, size = I(0.3), color = I("black"), linetype="solid") +
  geom_vline(xintercept = 1.5, size = I(0.3), color = I("black"), linetype="dashed") +
  geom_vline(xintercept = 2.5, size = I(0.3), color = I("black"), linetype="dashed") +
  geom_vline(xintercept = 3.5, size = I(0.3), color = I("black"), linetype="dashed") +
  geom_vline(xintercept = 4.5, size = I(0.3), color = I("black"), linetype="dashed") +
  ggpubr::grids(linetype = "dashed") +
  theme(strip.text.x = element_text(size = 12, colour = "black")) +
  theme(legend.position="bottom")

coef_plot_data

ggsave(filename = "coef_plot_data.jpg",plot = coef_plot_data, width = 15, height = 7.5, dpi = 1000)


####  Online Appendices, Table 3 Random effects of sleep duration and chronotype on turnout for each country. ####

ranef(issp_2007_turnout_sleep_duration_model1)
arm::se.ranef(issp_2007_turnout_sleep_duration_model1)

ranef(issp_2007_turnout_sleep_duration_model1)$country
ranef_issp_2007_turnout_sleep_duration_model1 <- data.frame(ranef(issp_2007_turnout_sleep_duration_model1)$country)
ranef_issp_2007_turnout_sleep_duration_model1 <- subset(ranef_issp_2007_turnout_sleep_duration_model1, select = -c(X.Intercept.))
ranef_issp_2007_turnout_sleep_duration_model1$country <- row.names(ranef_issp_2007_turnout_sleep_duration_model1)
names(ranef_issp_2007_turnout_sleep_duration_model1)[names(ranef_issp_2007_turnout_sleep_duration_model1)=="sleep_duration_beta"] <- "ranef_sleep_duration_beta"
names(ranef_issp_2007_turnout_sleep_duration_model1)[names(ranef_issp_2007_turnout_sleep_duration_model1)=="chronotype_interval_beta"] <- "ranef_chronotype_interval_beta"


se_ranef_issp_2007_turnout_sleep_duration_model1 <- data.frame(arm::se.ranef(issp_2007_turnout_sleep_duration_model1)$country)
se_ranef_issp_2007_turnout_sleep_duration_model1 <- subset(se_ranef_issp_2007_turnout_sleep_duration_model1, select = -c(X.Intercept.))
se_ranef_issp_2007_turnout_sleep_duration_model1$country <- row.names(se_ranef_issp_2007_turnout_sleep_duration_model1)
names(se_ranef_issp_2007_turnout_sleep_duration_model1)[names(se_ranef_issp_2007_turnout_sleep_duration_model1)=="sleep_duration_beta"] <- "se_sleep_duration_beta"
names(se_ranef_issp_2007_turnout_sleep_duration_model1)[names(se_ranef_issp_2007_turnout_sleep_duration_model1)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

ranef_se_issp_2007_turnout_sleep_duration_model1 <- merge(ranef_issp_2007_turnout_sleep_duration_model1, se_ranef_issp_2007_turnout_sleep_duration_model1, by="country")

ranef_se_issp_2007_turnout_sleep_duration_model1$slp.dur.cl.90 <- ranef_se_issp_2007_turnout_sleep_duration_model1$ranef_sleep_duration_beta - 1.645*ranef_se_issp_2007_turnout_sleep_duration_model1$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model1$slp.dur.cu.90 <- ranef_se_issp_2007_turnout_sleep_duration_model1$ranef_sleep_duration_beta + 1.645*ranef_se_issp_2007_turnout_sleep_duration_model1$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model1$slp.dur.cl.95 <- ranef_se_issp_2007_turnout_sleep_duration_model1$ranef_sleep_duration_beta - 1.96*ranef_se_issp_2007_turnout_sleep_duration_model1$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model1$slp.dur.cu.95 <- ranef_se_issp_2007_turnout_sleep_duration_model1$ranef_sleep_duration_beta + 1.96*ranef_se_issp_2007_turnout_sleep_duration_model1$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model1$slp.dur.cl.99 <- ranef_se_issp_2007_turnout_sleep_duration_model1$ranef_sleep_duration_beta - 2.576*ranef_se_issp_2007_turnout_sleep_duration_model1$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model1$slp.dur.cu.99 <- ranef_se_issp_2007_turnout_sleep_duration_model1$ranef_sleep_duration_beta + 2.576*ranef_se_issp_2007_turnout_sleep_duration_model1$se_sleep_duration_beta

ranef_se_issp_2007_turnout_sleep_duration_model1$chro.cl.90 <- ranef_se_issp_2007_turnout_sleep_duration_model1$ranef_chronotype_interval_beta - 1.645*ranef_se_issp_2007_turnout_sleep_duration_model1$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model1$chro.cu.90 <- ranef_se_issp_2007_turnout_sleep_duration_model1$ranef_chronotype_interval_beta + 1.645*ranef_se_issp_2007_turnout_sleep_duration_model1$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model1$chro.cl.95 <- ranef_se_issp_2007_turnout_sleep_duration_model1$ranef_chronotype_interval_beta - 1.96*ranef_se_issp_2007_turnout_sleep_duration_model1$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model1$chro.cu.95 <- ranef_se_issp_2007_turnout_sleep_duration_model1$ranef_chronotype_interval_beta + 1.96*ranef_se_issp_2007_turnout_sleep_duration_model1$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model1$chro.cl.99 <- ranef_se_issp_2007_turnout_sleep_duration_model1$ranef_chronotype_interval_beta - 2.576*ranef_se_issp_2007_turnout_sleep_duration_model1$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model1$chro.cu.99 <- ranef_se_issp_2007_turnout_sleep_duration_model1$ranef_chronotype_interval_beta + 2.576*ranef_se_issp_2007_turnout_sleep_duration_model1$se_chronotype_interval_beta

ranef_se_issp_2007_turnout_sleep_duration_model1$ranef_sleep_duration_beta <- round(ranef_se_issp_2007_turnout_sleep_duration_model1$ranef_sleep_duration_beta, 2)
ranef_se_issp_2007_turnout_sleep_duration_model1$se_sleep_duration_beta <- round(ranef_se_issp_2007_turnout_sleep_duration_model1$se_sleep_duration_beta, 2)
ranef_se_issp_2007_turnout_sleep_duration_model1$ranef_chronotype_interval_beta <- round(ranef_se_issp_2007_turnout_sleep_duration_model1$ranef_chronotype_interval_beta, 2)
ranef_se_issp_2007_turnout_sleep_duration_model1$se_chronotype_interval_beta <- round(ranef_se_issp_2007_turnout_sleep_duration_model1$se_chronotype_interval_beta, 2)
View(ranef_se_issp_2007_turnout_sleep_duration_model1)

ranef(issp_2007_turnout_sleep_duration_model2)
arm::se.ranef(issp_2007_turnout_sleep_duration_model2)

ranef(issp_2007_turnout_sleep_duration_model2)$country
ranef_issp_2007_turnout_sleep_duration_model2 <- data.frame(ranef(issp_2007_turnout_sleep_duration_model2)$country)
ranef_issp_2007_turnout_sleep_duration_model2 <- subset(ranef_issp_2007_turnout_sleep_duration_model2, select = c("sleep_duration_beta", "chronotype_interval_beta"))
ranef_issp_2007_turnout_sleep_duration_model2$country <- row.names(ranef_issp_2007_turnout_sleep_duration_model2)
names(ranef_issp_2007_turnout_sleep_duration_model2)[names(ranef_issp_2007_turnout_sleep_duration_model2)=="sleep_duration_beta"] <- "ranef_sleep_duration_beta"
names(ranef_issp_2007_turnout_sleep_duration_model2)[names(ranef_issp_2007_turnout_sleep_duration_model2)=="chronotype_interval_beta"] <- "ranef_chronotype_interval_beta"


se_ranef_issp_2007_turnout_sleep_duration_model2 <- data.frame(arm::se.ranef(issp_2007_turnout_sleep_duration_model2)$country)
se_ranef_issp_2007_turnout_sleep_duration_model2 <- subset(se_ranef_issp_2007_turnout_sleep_duration_model2, select = c("sleep_duration_beta", "chronotype_interval_beta"))
se_ranef_issp_2007_turnout_sleep_duration_model2$country <- row.names(se_ranef_issp_2007_turnout_sleep_duration_model2)
names(se_ranef_issp_2007_turnout_sleep_duration_model2)[names(se_ranef_issp_2007_turnout_sleep_duration_model2)=="sleep_duration_beta"] <- "se_sleep_duration_beta"
names(se_ranef_issp_2007_turnout_sleep_duration_model2)[names(se_ranef_issp_2007_turnout_sleep_duration_model2)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

ranef_se_issp_2007_turnout_sleep_duration_model2 <- merge(ranef_issp_2007_turnout_sleep_duration_model2, se_ranef_issp_2007_turnout_sleep_duration_model2, by="country")

ranef_se_issp_2007_turnout_sleep_duration_model2$slp.dur.cl.90 <- ranef_se_issp_2007_turnout_sleep_duration_model2$ranef_sleep_duration_beta - 1.645*ranef_se_issp_2007_turnout_sleep_duration_model2$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model2$slp.dur.cu.90 <- ranef_se_issp_2007_turnout_sleep_duration_model2$ranef_sleep_duration_beta + 1.645*ranef_se_issp_2007_turnout_sleep_duration_model2$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model2$slp.dur.cl.95 <- ranef_se_issp_2007_turnout_sleep_duration_model2$ranef_sleep_duration_beta - 1.96*ranef_se_issp_2007_turnout_sleep_duration_model2$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model2$slp.dur.cu.95 <- ranef_se_issp_2007_turnout_sleep_duration_model2$ranef_sleep_duration_beta + 1.96*ranef_se_issp_2007_turnout_sleep_duration_model2$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model2$slp.dur.cl.99 <- ranef_se_issp_2007_turnout_sleep_duration_model2$ranef_sleep_duration_beta - 2.576*ranef_se_issp_2007_turnout_sleep_duration_model2$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model2$slp.dur.cu.99 <- ranef_se_issp_2007_turnout_sleep_duration_model2$ranef_sleep_duration_beta + 2.576*ranef_se_issp_2007_turnout_sleep_duration_model2$se_sleep_duration_beta

ranef_se_issp_2007_turnout_sleep_duration_model2$chro.cl.90 <- ranef_se_issp_2007_turnout_sleep_duration_model2$ranef_chronotype_interval_beta - 1.645*ranef_se_issp_2007_turnout_sleep_duration_model2$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model2$chro.cu.90 <- ranef_se_issp_2007_turnout_sleep_duration_model2$ranef_chronotype_interval_beta + 1.645*ranef_se_issp_2007_turnout_sleep_duration_model2$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model2$chro.cl.95 <- ranef_se_issp_2007_turnout_sleep_duration_model2$ranef_chronotype_interval_beta - 1.96*ranef_se_issp_2007_turnout_sleep_duration_model2$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model2$chro.cu.95 <- ranef_se_issp_2007_turnout_sleep_duration_model2$ranef_chronotype_interval_beta + 1.96*ranef_se_issp_2007_turnout_sleep_duration_model2$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model2$chro.cl.99 <- ranef_se_issp_2007_turnout_sleep_duration_model2$ranef_chronotype_interval_beta - 2.576*ranef_se_issp_2007_turnout_sleep_duration_model2$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model2$chro.cu.99 <- ranef_se_issp_2007_turnout_sleep_duration_model2$ranef_chronotype_interval_beta + 2.576*ranef_se_issp_2007_turnout_sleep_duration_model2$se_chronotype_interval_beta

ranef_se_issp_2007_turnout_sleep_duration_model2$ranef_sleep_duration_beta <- round(ranef_se_issp_2007_turnout_sleep_duration_model2$ranef_sleep_duration_beta, 2)
ranef_se_issp_2007_turnout_sleep_duration_model2$se_sleep_duration_beta <- round(ranef_se_issp_2007_turnout_sleep_duration_model2$se_sleep_duration_beta, 2)
ranef_se_issp_2007_turnout_sleep_duration_model2$ranef_chronotype_interval_beta <- round(ranef_se_issp_2007_turnout_sleep_duration_model2$ranef_chronotype_interval_beta, 2)
ranef_se_issp_2007_turnout_sleep_duration_model2$se_chronotype_interval_beta <- round(ranef_se_issp_2007_turnout_sleep_duration_model2$se_chronotype_interval_beta, 2)
View(ranef_se_issp_2007_turnout_sleep_duration_model2)

ranef(issp_2007_turnout_sleep_duration_model3)
arm::se.ranef(issp_2007_turnout_sleep_duration_model3)

ranef(issp_2007_turnout_sleep_duration_model3)$country
ranef_issp_2007_turnout_sleep_duration_model3 <- data.frame(ranef(issp_2007_turnout_sleep_duration_model3)$country)
ranef_issp_2007_turnout_sleep_duration_model3 <- subset(ranef_issp_2007_turnout_sleep_duration_model3, select = c("sleep_duration_beta", "chronotype_interval_beta"))
ranef_issp_2007_turnout_sleep_duration_model3$country <- row.names(ranef_issp_2007_turnout_sleep_duration_model3)
names(ranef_issp_2007_turnout_sleep_duration_model3)[names(ranef_issp_2007_turnout_sleep_duration_model3)=="sleep_duration_beta"] <- "ranef_sleep_duration_beta"
names(ranef_issp_2007_turnout_sleep_duration_model3)[names(ranef_issp_2007_turnout_sleep_duration_model3)=="chronotype_interval_beta"] <- "ranef_chronotype_interval_beta"


se_ranef_issp_2007_turnout_sleep_duration_model3 <- data.frame(arm::se.ranef(issp_2007_turnout_sleep_duration_model3)$country)
se_ranef_issp_2007_turnout_sleep_duration_model3 <- subset(se_ranef_issp_2007_turnout_sleep_duration_model3, select = c("sleep_duration_beta", "chronotype_interval_beta"))
se_ranef_issp_2007_turnout_sleep_duration_model3$country <- row.names(se_ranef_issp_2007_turnout_sleep_duration_model3)
names(se_ranef_issp_2007_turnout_sleep_duration_model3)[names(se_ranef_issp_2007_turnout_sleep_duration_model3)=="sleep_duration_beta"] <- "se_sleep_duration_beta"
names(se_ranef_issp_2007_turnout_sleep_duration_model3)[names(se_ranef_issp_2007_turnout_sleep_duration_model3)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

ranef_se_issp_2007_turnout_sleep_duration_model3 <- merge(ranef_issp_2007_turnout_sleep_duration_model3, se_ranef_issp_2007_turnout_sleep_duration_model3, by="country")

ranef_se_issp_2007_turnout_sleep_duration_model3$slp.dur.cl.90 <- ranef_se_issp_2007_turnout_sleep_duration_model3$ranef_sleep_duration_beta - 1.645*ranef_se_issp_2007_turnout_sleep_duration_model3$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model3$slp.dur.cu.90 <- ranef_se_issp_2007_turnout_sleep_duration_model3$ranef_sleep_duration_beta + 1.645*ranef_se_issp_2007_turnout_sleep_duration_model3$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model3$slp.dur.cl.95 <- ranef_se_issp_2007_turnout_sleep_duration_model3$ranef_sleep_duration_beta - 1.96*ranef_se_issp_2007_turnout_sleep_duration_model3$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model3$slp.dur.cu.95 <- ranef_se_issp_2007_turnout_sleep_duration_model3$ranef_sleep_duration_beta + 1.96*ranef_se_issp_2007_turnout_sleep_duration_model3$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model3$slp.dur.cl.99 <- ranef_se_issp_2007_turnout_sleep_duration_model3$ranef_sleep_duration_beta - 2.576*ranef_se_issp_2007_turnout_sleep_duration_model3$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model3$slp.dur.cu.99 <- ranef_se_issp_2007_turnout_sleep_duration_model3$ranef_sleep_duration_beta + 2.576*ranef_se_issp_2007_turnout_sleep_duration_model3$se_sleep_duration_beta

ranef_se_issp_2007_turnout_sleep_duration_model3$chro.cl.90 <- ranef_se_issp_2007_turnout_sleep_duration_model3$ranef_chronotype_interval_beta - 1.645*ranef_se_issp_2007_turnout_sleep_duration_model3$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model3$chro.cu.90 <- ranef_se_issp_2007_turnout_sleep_duration_model3$ranef_chronotype_interval_beta + 1.645*ranef_se_issp_2007_turnout_sleep_duration_model3$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model3$chro.cl.95 <- ranef_se_issp_2007_turnout_sleep_duration_model3$ranef_chronotype_interval_beta - 1.96*ranef_se_issp_2007_turnout_sleep_duration_model3$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model3$chro.cu.95 <- ranef_se_issp_2007_turnout_sleep_duration_model3$ranef_chronotype_interval_beta + 1.96*ranef_se_issp_2007_turnout_sleep_duration_model3$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model3$chro.cl.99 <- ranef_se_issp_2007_turnout_sleep_duration_model3$ranef_chronotype_interval_beta - 2.576*ranef_se_issp_2007_turnout_sleep_duration_model3$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model3$chro.cu.99 <- ranef_se_issp_2007_turnout_sleep_duration_model3$ranef_chronotype_interval_beta + 2.576*ranef_se_issp_2007_turnout_sleep_duration_model3$se_chronotype_interval_beta

ranef_se_issp_2007_turnout_sleep_duration_model3$ranef_sleep_duration_beta <- round(ranef_se_issp_2007_turnout_sleep_duration_model3$ranef_sleep_duration_beta, 2)
ranef_se_issp_2007_turnout_sleep_duration_model3$se_sleep_duration_beta <- round(ranef_se_issp_2007_turnout_sleep_duration_model3$se_sleep_duration_beta, 2)
ranef_se_issp_2007_turnout_sleep_duration_model3$ranef_chronotype_interval_beta <- round(ranef_se_issp_2007_turnout_sleep_duration_model3$ranef_chronotype_interval_beta, 2)
ranef_se_issp_2007_turnout_sleep_duration_model3$se_chronotype_interval_beta <- round(ranef_se_issp_2007_turnout_sleep_duration_model3$se_chronotype_interval_beta, 2)
View(ranef_se_issp_2007_turnout_sleep_duration_model3)

ranef(issp_2007_turnout_sleep_duration_model4)
arm::se.ranef(issp_2007_turnout_sleep_duration_model4)

ranef(issp_2007_turnout_sleep_duration_model4)$country
ranef_issp_2007_turnout_sleep_duration_model4 <- data.frame(ranef(issp_2007_turnout_sleep_duration_model4)$country)
ranef_issp_2007_turnout_sleep_duration_model4 <- subset(ranef_issp_2007_turnout_sleep_duration_model4, select = c("sleep_duration_beta", "chronotype_interval_beta"))
ranef_issp_2007_turnout_sleep_duration_model4$country <- row.names(ranef_issp_2007_turnout_sleep_duration_model4)
names(ranef_issp_2007_turnout_sleep_duration_model4)[names(ranef_issp_2007_turnout_sleep_duration_model4)=="sleep_duration_beta"] <- "ranef_sleep_duration_beta"
names(ranef_issp_2007_turnout_sleep_duration_model4)[names(ranef_issp_2007_turnout_sleep_duration_model4)=="chronotype_interval_beta"] <- "ranef_chronotype_interval_beta"


se_ranef_issp_2007_turnout_sleep_duration_model4 <- data.frame(arm::se.ranef(issp_2007_turnout_sleep_duration_model4)$country)
se_ranef_issp_2007_turnout_sleep_duration_model4 <- subset(se_ranef_issp_2007_turnout_sleep_duration_model4, select = c("sleep_duration_beta", "chronotype_interval_beta"))
se_ranef_issp_2007_turnout_sleep_duration_model4$country <- row.names(se_ranef_issp_2007_turnout_sleep_duration_model4)
names(se_ranef_issp_2007_turnout_sleep_duration_model4)[names(se_ranef_issp_2007_turnout_sleep_duration_model4)=="sleep_duration_beta"] <- "se_sleep_duration_beta"
names(se_ranef_issp_2007_turnout_sleep_duration_model4)[names(se_ranef_issp_2007_turnout_sleep_duration_model4)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

ranef_se_issp_2007_turnout_sleep_duration_model4 <- merge(ranef_issp_2007_turnout_sleep_duration_model4, se_ranef_issp_2007_turnout_sleep_duration_model4, by="country")

ranef_se_issp_2007_turnout_sleep_duration_model4$slp.dur.cl.90 <- ranef_se_issp_2007_turnout_sleep_duration_model4$ranef_sleep_duration_beta - 1.645*ranef_se_issp_2007_turnout_sleep_duration_model4$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model4$slp.dur.cu.90 <- ranef_se_issp_2007_turnout_sleep_duration_model4$ranef_sleep_duration_beta + 1.645*ranef_se_issp_2007_turnout_sleep_duration_model4$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model4$slp.dur.cl.95 <- ranef_se_issp_2007_turnout_sleep_duration_model4$ranef_sleep_duration_beta - 1.96*ranef_se_issp_2007_turnout_sleep_duration_model4$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model4$slp.dur.cu.95 <- ranef_se_issp_2007_turnout_sleep_duration_model4$ranef_sleep_duration_beta + 1.96*ranef_se_issp_2007_turnout_sleep_duration_model4$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model4$slp.dur.cl.99 <- ranef_se_issp_2007_turnout_sleep_duration_model4$ranef_sleep_duration_beta - 2.576*ranef_se_issp_2007_turnout_sleep_duration_model4$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model4$slp.dur.cu.99 <- ranef_se_issp_2007_turnout_sleep_duration_model4$ranef_sleep_duration_beta + 2.576*ranef_se_issp_2007_turnout_sleep_duration_model4$se_sleep_duration_beta

ranef_se_issp_2007_turnout_sleep_duration_model4$chro.cl.90 <- ranef_se_issp_2007_turnout_sleep_duration_model4$ranef_chronotype_interval_beta - 1.645*ranef_se_issp_2007_turnout_sleep_duration_model4$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model4$chro.cu.90 <- ranef_se_issp_2007_turnout_sleep_duration_model4$ranef_chronotype_interval_beta + 1.645*ranef_se_issp_2007_turnout_sleep_duration_model4$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model4$chro.cl.95 <- ranef_se_issp_2007_turnout_sleep_duration_model4$ranef_chronotype_interval_beta - 1.96*ranef_se_issp_2007_turnout_sleep_duration_model4$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model4$chro.cu.95 <- ranef_se_issp_2007_turnout_sleep_duration_model4$ranef_chronotype_interval_beta + 1.96*ranef_se_issp_2007_turnout_sleep_duration_model4$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model4$chro.cl.99 <- ranef_se_issp_2007_turnout_sleep_duration_model4$ranef_chronotype_interval_beta - 2.576*ranef_se_issp_2007_turnout_sleep_duration_model4$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model4$chro.cu.99 <- ranef_se_issp_2007_turnout_sleep_duration_model4$ranef_chronotype_interval_beta + 2.576*ranef_se_issp_2007_turnout_sleep_duration_model4$se_chronotype_interval_beta

ranef_se_issp_2007_turnout_sleep_duration_model4$ranef_sleep_duration_beta <- round(ranef_se_issp_2007_turnout_sleep_duration_model4$ranef_sleep_duration_beta, 2)
ranef_se_issp_2007_turnout_sleep_duration_model4$se_sleep_duration_beta <- round(ranef_se_issp_2007_turnout_sleep_duration_model4$se_sleep_duration_beta, 2)
ranef_se_issp_2007_turnout_sleep_duration_model4$ranef_chronotype_interval_beta <- round(ranef_se_issp_2007_turnout_sleep_duration_model4$ranef_chronotype_interval_beta, 2)
ranef_se_issp_2007_turnout_sleep_duration_model4$se_chronotype_interval_beta <- round(ranef_se_issp_2007_turnout_sleep_duration_model4$se_chronotype_interval_beta, 2)
View(ranef_se_issp_2007_turnout_sleep_duration_model4)

#### Online Appendices, Table 4 Multilevel logistic regression models of turnout by sleep duration and chronotype additionally accounting for day and season grouping effect ####

issp_2007_turnout_sleep_duration_model1_day_season <- glmer(turnout ~ sleep_duration_beta + chronotype_interval_beta + 
                                                              (1 + sleep_duration_beta + chronotype_interval_beta | country/local_election) +
                                                              (1 + sleep_duration_beta + chronotype_interval_beta | mid_field_season/day_off), 
                                                            data =  issp_2007, family = "binomial", nAGQ=0)
summary(issp_2007_turnout_sleep_duration_model1_day_season)
tab_model(issp_2007_turnout_sleep_duration_model1_day_season, show.ci = 0.95)

issp_2007_turnout_sleep_duration_model2_day_season <- glmer(turnout ~ sleep_duration_beta + chronotype_interval_beta + 
                                                              urban_rural_area_beta + (1  + sleep_duration_beta + chronotype_interval_beta | country/local_election) +
                                                              (1 + sleep_duration_beta + chronotype_interval_beta | mid_field_season/day_off), 
                                                            data =  issp_2007, family = "binomial", nAGQ=0)
summary(issp_2007_turnout_sleep_duration_model2_day_season)
tab_model(issp_2007_turnout_sleep_duration_model2_day_season, show.ci = 0.95)

issp_2007_turnout_sleep_duration_model3_day_season <- glmer(turnout ~ sleep_duration_beta + chronotype_interval_beta + 
                                                              urban_rural_area_beta +
                                                              age_beta + degree_beta + income_beta + sex_beta + political_interest_beta + PARTY_LR_beta + 
                                                              (1  + sleep_duration_beta + chronotype_interval_beta | country/local_election) +
                                                              (1 + sleep_duration_beta + chronotype_interval_beta | mid_field_season/day_off), 
                                                            data =  issp_2007, family = "binomial", nAGQ=0)
summary(issp_2007_turnout_sleep_duration_model3_day_season)
tab_model(issp_2007_turnout_sleep_duration_model3_day_season, show.ci = 0.95)

issp_2007_turnout_sleep_duration_model4_day_season <- glmer(turnout ~ sleep_duration_beta + chronotype_interval_beta + 
                                                              urban_rural_area_beta +
                                                              age_beta + degree_beta + income_beta + sex_beta + political_interest_beta + religiosity_beta + 
                                                              (1  + sleep_duration_beta + chronotype_interval_beta | country/local_election) +
                                                              (1 + sleep_duration_beta + chronotype_interval_beta | mid_field_season/day_off), 
                                                            data =  issp_2007, family = "binomial", nAGQ=0)
summary(issp_2007_turnout_sleep_duration_model4_day_season)
tab_model(issp_2007_turnout_sleep_duration_model4_day_season, show.ci = 0.95)

stargazer(issp_2007_turnout_sleep_duration_model1_day_season, issp_2007_turnout_sleep_duration_model2_day_season, issp_2007_turnout_sleep_duration_model3_day_season, issp_2007_turnout_sleep_duration_model4_day_season,
          type = "html", title=" ", digits=2, out="Tables/day_season_models_issp_2007_turnout_sleep_duration_chronotype.htm",
          model.numbers = F,
          column.labels = c("Model 1", "Model 2", "Model 3", "Model 4"),
          covariate.labels = c("Sleep Duration", 
                               "Chronotype",
                               "Urban-Rural Area of Residence", 
                               "Age", 
                               "Education", 
                               "Income", 
                               "Sex: Male (Base: Female)",
                               "Level on Interest in Politics", 
                               "Left-Right Ideological Placement", 
                               "Religious Attendance", 
                               "Intercept"))

####  Online Appendices, Table 5 Predicted effects of sleep duration and chronotype on turnout for each country from the model additionally accounting for day and season grouping effect ####

coef(issp_2007_turnout_sleep_duration_model1_day_season)
arm::se.coef(issp_2007_turnout_sleep_duration_model1_day_season)

coef(issp_2007_turnout_sleep_duration_model1_day_season)$country
coef_issp_2007_turnout_sleep_duration_model1_day_season <- data.frame(coef(issp_2007_turnout_sleep_duration_model1_day_season)$country)
coef_issp_2007_turnout_sleep_duration_model1_day_season <- subset(coef_issp_2007_turnout_sleep_duration_model1_day_season, select = c("sleep_duration_beta", "chronotype_interval_beta"))
coef_issp_2007_turnout_sleep_duration_model1_day_season$country <- row.names(coef_issp_2007_turnout_sleep_duration_model1_day_season)
names(coef_issp_2007_turnout_sleep_duration_model1_day_season)[names(coef_issp_2007_turnout_sleep_duration_model1_day_season)=="sleep_duration_beta"] <- "coef_sleep_duration_beta"
names(coef_issp_2007_turnout_sleep_duration_model1_day_season)[names(coef_issp_2007_turnout_sleep_duration_model1_day_season)=="chronotype_interval_beta"] <- "coef_chronotype_interval_beta"


se_coef_issp_2007_turnout_sleep_duration_model1_day_season <- data.frame(arm::se.coef(issp_2007_turnout_sleep_duration_model1_day_season)$country)
se_coef_issp_2007_turnout_sleep_duration_model1_day_season <- subset(se_coef_issp_2007_turnout_sleep_duration_model1_day_season, select = c("sleep_duration_beta", "chronotype_interval_beta"))
se_coef_issp_2007_turnout_sleep_duration_model1_day_season$country <- row.names(se_coef_issp_2007_turnout_sleep_duration_model1_day_season)
names(se_coef_issp_2007_turnout_sleep_duration_model1_day_season)[names(se_coef_issp_2007_turnout_sleep_duration_model1_day_season)=="sleep_duration_beta"] <- "se_sleep_duration_beta"
names(se_coef_issp_2007_turnout_sleep_duration_model1_day_season)[names(se_coef_issp_2007_turnout_sleep_duration_model1_day_season)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

coef_se_issp_2007_turnout_sleep_duration_model1_day_season <- merge(coef_issp_2007_turnout_sleep_duration_model1_day_season, se_coef_issp_2007_turnout_sleep_duration_model1_day_season, by="country")

coef_se_issp_2007_turnout_sleep_duration_model1_day_season$slp.dur.cl.90 <- coef_se_issp_2007_turnout_sleep_duration_model1_day_season$coef_sleep_duration_beta - 1.645*coef_se_issp_2007_turnout_sleep_duration_model1_day_season$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model1_day_season$slp.dur.cu.90 <- coef_se_issp_2007_turnout_sleep_duration_model1_day_season$coef_sleep_duration_beta + 1.645*coef_se_issp_2007_turnout_sleep_duration_model1_day_season$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model1_day_season$slp.dur.cl.95 <- coef_se_issp_2007_turnout_sleep_duration_model1_day_season$coef_sleep_duration_beta - 1.96*coef_se_issp_2007_turnout_sleep_duration_model1_day_season$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model1_day_season$slp.dur.cu.95 <- coef_se_issp_2007_turnout_sleep_duration_model1_day_season$coef_sleep_duration_beta + 1.96*coef_se_issp_2007_turnout_sleep_duration_model1_day_season$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model1_day_season$slp.dur.cl.99 <- coef_se_issp_2007_turnout_sleep_duration_model1_day_season$coef_sleep_duration_beta - 2.576*coef_se_issp_2007_turnout_sleep_duration_model1_day_season$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model1_day_season$slp.dur.cu.99 <- coef_se_issp_2007_turnout_sleep_duration_model1_day_season$coef_sleep_duration_beta + 2.576*coef_se_issp_2007_turnout_sleep_duration_model1_day_season$se_sleep_duration_beta

coef_se_issp_2007_turnout_sleep_duration_model1_day_season$chro.cl.90 <- coef_se_issp_2007_turnout_sleep_duration_model1_day_season$coef_chronotype_interval_beta - 1.645*coef_se_issp_2007_turnout_sleep_duration_model1_day_season$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model1_day_season$chro.cu.90 <- coef_se_issp_2007_turnout_sleep_duration_model1_day_season$coef_chronotype_interval_beta + 1.645*coef_se_issp_2007_turnout_sleep_duration_model1_day_season$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model1_day_season$chro.cl.95 <- coef_se_issp_2007_turnout_sleep_duration_model1_day_season$coef_chronotype_interval_beta - 1.96*coef_se_issp_2007_turnout_sleep_duration_model1_day_season$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model1_day_season$chro.cu.95 <- coef_se_issp_2007_turnout_sleep_duration_model1_day_season$coef_chronotype_interval_beta + 1.96*coef_se_issp_2007_turnout_sleep_duration_model1_day_season$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model1_day_season$chro.cl.99 <- coef_se_issp_2007_turnout_sleep_duration_model1_day_season$coef_chronotype_interval_beta - 2.576*coef_se_issp_2007_turnout_sleep_duration_model1_day_season$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model1_day_season$chro.cu.99 <- coef_se_issp_2007_turnout_sleep_duration_model1_day_season$coef_chronotype_interval_beta + 2.576*coef_se_issp_2007_turnout_sleep_duration_model1_day_season$se_chronotype_interval_beta

coef_se_issp_2007_turnout_sleep_duration_model1_day_season$coef_sleep_duration_beta <- round(coef_se_issp_2007_turnout_sleep_duration_model1_day_season$coef_sleep_duration_beta, 2)
coef_se_issp_2007_turnout_sleep_duration_model1_day_season$se_sleep_duration_beta <- round(coef_se_issp_2007_turnout_sleep_duration_model1_day_season$se_sleep_duration_beta, 2)
coef_se_issp_2007_turnout_sleep_duration_model1_day_season$coef_chronotype_interval_beta <- round(coef_se_issp_2007_turnout_sleep_duration_model1_day_season$coef_chronotype_interval_beta, 2)
coef_se_issp_2007_turnout_sleep_duration_model1_day_season$se_chronotype_interval_beta <- round(coef_se_issp_2007_turnout_sleep_duration_model1_day_season$se_chronotype_interval_beta, 2)
View(coef_se_issp_2007_turnout_sleep_duration_model1_day_season)

coef(issp_2007_turnout_sleep_duration_model2_day_season)
arm::se.coef(issp_2007_turnout_sleep_duration_model2_day_season)

coef(issp_2007_turnout_sleep_duration_model2_day_season)$country
coef_issp_2007_turnout_sleep_duration_model2_day_season <- data.frame(coef(issp_2007_turnout_sleep_duration_model2_day_season)$country)
coef_issp_2007_turnout_sleep_duration_model2_day_season <- subset(coef_issp_2007_turnout_sleep_duration_model2_day_season, select = c("sleep_duration_beta", "chronotype_interval_beta"))
coef_issp_2007_turnout_sleep_duration_model2_day_season$country <- row.names(coef_issp_2007_turnout_sleep_duration_model2_day_season)
names(coef_issp_2007_turnout_sleep_duration_model2_day_season)[names(coef_issp_2007_turnout_sleep_duration_model2_day_season)=="sleep_duration_beta"] <- "coef_sleep_duration_beta"
names(coef_issp_2007_turnout_sleep_duration_model2_day_season)[names(coef_issp_2007_turnout_sleep_duration_model2_day_season)=="chronotype_interval_beta"] <- "coef_chronotype_interval_beta"


se_coef_issp_2007_turnout_sleep_duration_model2_day_season <- data.frame(arm::se.coef(issp_2007_turnout_sleep_duration_model2_day_season)$country)
se_coef_issp_2007_turnout_sleep_duration_model2_day_season <- subset(se_coef_issp_2007_turnout_sleep_duration_model2_day_season, select = c("sleep_duration_beta", "chronotype_interval_beta"))
se_coef_issp_2007_turnout_sleep_duration_model2_day_season$country <- row.names(se_coef_issp_2007_turnout_sleep_duration_model2_day_season)
names(se_coef_issp_2007_turnout_sleep_duration_model2_day_season)[names(se_coef_issp_2007_turnout_sleep_duration_model2_day_season)=="sleep_duration_beta"] <- "se_sleep_duration_beta"
names(se_coef_issp_2007_turnout_sleep_duration_model2_day_season)[names(se_coef_issp_2007_turnout_sleep_duration_model2_day_season)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

coef_se_issp_2007_turnout_sleep_duration_model2_day_season <- merge(coef_issp_2007_turnout_sleep_duration_model2_day_season, se_coef_issp_2007_turnout_sleep_duration_model2_day_season, by="country")

coef_se_issp_2007_turnout_sleep_duration_model2_day_season$slp.dur.cl.90 <- coef_se_issp_2007_turnout_sleep_duration_model2_day_season$coef_sleep_duration_beta - 1.645*coef_se_issp_2007_turnout_sleep_duration_model2_day_season$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model2_day_season$slp.dur.cu.90 <- coef_se_issp_2007_turnout_sleep_duration_model2_day_season$coef_sleep_duration_beta + 1.645*coef_se_issp_2007_turnout_sleep_duration_model2_day_season$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model2_day_season$slp.dur.cl.95 <- coef_se_issp_2007_turnout_sleep_duration_model2_day_season$coef_sleep_duration_beta - 1.96*coef_se_issp_2007_turnout_sleep_duration_model2_day_season$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model2_day_season$slp.dur.cu.95 <- coef_se_issp_2007_turnout_sleep_duration_model2_day_season$coef_sleep_duration_beta + 1.96*coef_se_issp_2007_turnout_sleep_duration_model2_day_season$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model2_day_season$slp.dur.cl.99 <- coef_se_issp_2007_turnout_sleep_duration_model2_day_season$coef_sleep_duration_beta - 2.576*coef_se_issp_2007_turnout_sleep_duration_model2_day_season$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model2_day_season$slp.dur.cu.99 <- coef_se_issp_2007_turnout_sleep_duration_model2_day_season$coef_sleep_duration_beta + 2.576*coef_se_issp_2007_turnout_sleep_duration_model2_day_season$se_sleep_duration_beta

coef_se_issp_2007_turnout_sleep_duration_model2_day_season$chro.cl.90 <- coef_se_issp_2007_turnout_sleep_duration_model2_day_season$coef_chronotype_interval_beta - 1.645*coef_se_issp_2007_turnout_sleep_duration_model2_day_season$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model2_day_season$chro.cu.90 <- coef_se_issp_2007_turnout_sleep_duration_model2_day_season$coef_chronotype_interval_beta + 1.645*coef_se_issp_2007_turnout_sleep_duration_model2_day_season$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model2_day_season$chro.cl.95 <- coef_se_issp_2007_turnout_sleep_duration_model2_day_season$coef_chronotype_interval_beta - 1.96*coef_se_issp_2007_turnout_sleep_duration_model2_day_season$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model2_day_season$chro.cu.95 <- coef_se_issp_2007_turnout_sleep_duration_model2_day_season$coef_chronotype_interval_beta + 1.96*coef_se_issp_2007_turnout_sleep_duration_model2_day_season$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model2_day_season$chro.cl.99 <- coef_se_issp_2007_turnout_sleep_duration_model2_day_season$coef_chronotype_interval_beta - 2.576*coef_se_issp_2007_turnout_sleep_duration_model2_day_season$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model2_day_season$chro.cu.99 <- coef_se_issp_2007_turnout_sleep_duration_model2_day_season$coef_chronotype_interval_beta + 2.576*coef_se_issp_2007_turnout_sleep_duration_model2_day_season$se_chronotype_interval_beta

coef_se_issp_2007_turnout_sleep_duration_model2_day_season$coef_sleep_duration_beta <- round(coef_se_issp_2007_turnout_sleep_duration_model2_day_season$coef_sleep_duration_beta, 2)
coef_se_issp_2007_turnout_sleep_duration_model2_day_season$se_sleep_duration_beta <- round(coef_se_issp_2007_turnout_sleep_duration_model2_day_season$se_sleep_duration_beta, 2)
coef_se_issp_2007_turnout_sleep_duration_model2_day_season$coef_chronotype_interval_beta <- round(coef_se_issp_2007_turnout_sleep_duration_model2_day_season$coef_chronotype_interval_beta, 2)
coef_se_issp_2007_turnout_sleep_duration_model2_day_season$se_chronotype_interval_beta <- round(coef_se_issp_2007_turnout_sleep_duration_model2_day_season$se_chronotype_interval_beta, 2)
View(coef_se_issp_2007_turnout_sleep_duration_model2_day_season)

coef(issp_2007_turnout_sleep_duration_model3_day_season)
arm::se.coef(issp_2007_turnout_sleep_duration_model3_day_season)

coef(issp_2007_turnout_sleep_duration_model3_day_season)$country
coef_issp_2007_turnout_sleep_duration_model3_day_season <- data.frame(coef(issp_2007_turnout_sleep_duration_model3_day_season)$country)
coef_issp_2007_turnout_sleep_duration_model3_day_season <- subset(coef_issp_2007_turnout_sleep_duration_model3_day_season, select = c("sleep_duration_beta", "chronotype_interval_beta"))
coef_issp_2007_turnout_sleep_duration_model3_day_season$country <- row.names(coef_issp_2007_turnout_sleep_duration_model3_day_season)
names(coef_issp_2007_turnout_sleep_duration_model3_day_season)[names(coef_issp_2007_turnout_sleep_duration_model3_day_season)=="sleep_duration_beta"] <- "coef_sleep_duration_beta"
names(coef_issp_2007_turnout_sleep_duration_model3_day_season)[names(coef_issp_2007_turnout_sleep_duration_model3_day_season)=="chronotype_interval_beta"] <- "coef_chronotype_interval_beta"


se_coef_issp_2007_turnout_sleep_duration_model3_day_season <- data.frame(arm::se.coef(issp_2007_turnout_sleep_duration_model3_day_season)$country)
se_coef_issp_2007_turnout_sleep_duration_model3_day_season <- subset(se_coef_issp_2007_turnout_sleep_duration_model3_day_season, select = c("sleep_duration_beta", "chronotype_interval_beta"))
se_coef_issp_2007_turnout_sleep_duration_model3_day_season$country <- row.names(se_coef_issp_2007_turnout_sleep_duration_model3_day_season)
names(se_coef_issp_2007_turnout_sleep_duration_model3_day_season)[names(se_coef_issp_2007_turnout_sleep_duration_model3_day_season)=="sleep_duration_beta"] <- "se_sleep_duration_beta"
names(se_coef_issp_2007_turnout_sleep_duration_model3_day_season)[names(se_coef_issp_2007_turnout_sleep_duration_model3_day_season)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

coef_se_issp_2007_turnout_sleep_duration_model3_day_season <- merge(coef_issp_2007_turnout_sleep_duration_model3_day_season, se_coef_issp_2007_turnout_sleep_duration_model3_day_season, by="country")

coef_se_issp_2007_turnout_sleep_duration_model3_day_season$slp.dur.cl.90 <- coef_se_issp_2007_turnout_sleep_duration_model3_day_season$coef_sleep_duration_beta - 1.645*coef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model3_day_season$slp.dur.cu.90 <- coef_se_issp_2007_turnout_sleep_duration_model3_day_season$coef_sleep_duration_beta + 1.645*coef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model3_day_season$slp.dur.cl.95 <- coef_se_issp_2007_turnout_sleep_duration_model3_day_season$coef_sleep_duration_beta - 1.96*coef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model3_day_season$slp.dur.cu.95 <- coef_se_issp_2007_turnout_sleep_duration_model3_day_season$coef_sleep_duration_beta + 1.96*coef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model3_day_season$slp.dur.cl.99 <- coef_se_issp_2007_turnout_sleep_duration_model3_day_season$coef_sleep_duration_beta - 2.576*coef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model3_day_season$slp.dur.cu.99 <- coef_se_issp_2007_turnout_sleep_duration_model3_day_season$coef_sleep_duration_beta + 2.576*coef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_sleep_duration_beta

coef_se_issp_2007_turnout_sleep_duration_model3_day_season$chro.cl.90 <- coef_se_issp_2007_turnout_sleep_duration_model3_day_season$coef_chronotype_interval_beta - 1.645*coef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model3_day_season$chro.cu.90 <- coef_se_issp_2007_turnout_sleep_duration_model3_day_season$coef_chronotype_interval_beta + 1.645*coef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model3_day_season$chro.cl.95 <- coef_se_issp_2007_turnout_sleep_duration_model3_day_season$coef_chronotype_interval_beta - 1.96*coef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model3_day_season$chro.cu.95 <- coef_se_issp_2007_turnout_sleep_duration_model3_day_season$coef_chronotype_interval_beta + 1.96*coef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model3_day_season$chro.cl.99 <- coef_se_issp_2007_turnout_sleep_duration_model3_day_season$coef_chronotype_interval_beta - 2.576*coef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model3_day_season$chro.cu.99 <- coef_se_issp_2007_turnout_sleep_duration_model3_day_season$coef_chronotype_interval_beta + 2.576*coef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_chronotype_interval_beta

coef(issp_2007_turnout_sleep_duration_model3_day_season)$country
coef_issp_2007_turnout_sleep_duration_model3_day_season <- data.frame(coef(issp_2007_turnout_sleep_duration_model3_day_season)$country)
coef_issp_2007_turnout_sleep_duration_model3_day_season <- subset(coef_issp_2007_turnout_sleep_duration_model3_day_season, select = c("sleep_duration_beta", "chronotype_interval_beta"))
coef_issp_2007_turnout_sleep_duration_model3_day_season$country <- row.names(coef_issp_2007_turnout_sleep_duration_model3_day_season)
names(coef_issp_2007_turnout_sleep_duration_model3_day_season)[names(coef_issp_2007_turnout_sleep_duration_model3_day_season)=="sleep_duration_beta"] <- "coef_sleep_duration_beta"
names(coef_issp_2007_turnout_sleep_duration_model3_day_season)[names(coef_issp_2007_turnout_sleep_duration_model3_day_season)=="chronotype_interval_beta"] <- "coef_chronotype_interval_beta"


se_coef_issp_2007_turnout_sleep_duration_model3_day_season <- data.frame(arm::se.coef(issp_2007_turnout_sleep_duration_model3_day_season)$country)
se_coef_issp_2007_turnout_sleep_duration_model3_day_season <- subset(se_coef_issp_2007_turnout_sleep_duration_model3_day_season, select = c("sleep_duration_beta", "chronotype_interval_beta"))
se_coef_issp_2007_turnout_sleep_duration_model3_day_season$country <- row.names(se_coef_issp_2007_turnout_sleep_duration_model3_day_season)
names(se_coef_issp_2007_turnout_sleep_duration_model3_day_season)[names(se_coef_issp_2007_turnout_sleep_duration_model3_day_season)=="sleep_duration_beta"] <- "se_sleep_duration_beta"
names(se_coef_issp_2007_turnout_sleep_duration_model3_day_season)[names(se_coef_issp_2007_turnout_sleep_duration_model3_day_season)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

coef_se_issp_2007_turnout_sleep_duration_model3_day_season <- merge(coef_issp_2007_turnout_sleep_duration_model3_day_season, se_coef_issp_2007_turnout_sleep_duration_model3_day_season, by="country")

coef_se_issp_2007_turnout_sleep_duration_model3_day_season$slp.dur.cl.90 <- coef_se_issp_2007_turnout_sleep_duration_model3_day_season$coef_sleep_duration_beta - 1.645*coef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model3_day_season$slp.dur.cu.90 <- coef_se_issp_2007_turnout_sleep_duration_model3_day_season$coef_sleep_duration_beta + 1.645*coef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model3_day_season$slp.dur.cl.95 <- coef_se_issp_2007_turnout_sleep_duration_model3_day_season$coef_sleep_duration_beta - 1.96*coef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model3_day_season$slp.dur.cu.95 <- coef_se_issp_2007_turnout_sleep_duration_model3_day_season$coef_sleep_duration_beta + 1.96*coef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model3_day_season$slp.dur.cl.99 <- coef_se_issp_2007_turnout_sleep_duration_model3_day_season$coef_sleep_duration_beta - 2.576*coef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model3_day_season$slp.dur.cu.99 <- coef_se_issp_2007_turnout_sleep_duration_model3_day_season$coef_sleep_duration_beta + 2.576*coef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_sleep_duration_beta

coef_se_issp_2007_turnout_sleep_duration_model3_day_season$chro.cl.90 <- coef_se_issp_2007_turnout_sleep_duration_model3_day_season$coef_chronotype_interval_beta - 1.645*coef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model3_day_season$chro.cu.90 <- coef_se_issp_2007_turnout_sleep_duration_model3_day_season$coef_chronotype_interval_beta + 1.645*coef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model3_day_season$chro.cl.95 <- coef_se_issp_2007_turnout_sleep_duration_model3_day_season$coef_chronotype_interval_beta - 1.96*coef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model3_day_season$chro.cu.95 <- coef_se_issp_2007_turnout_sleep_duration_model3_day_season$coef_chronotype_interval_beta + 1.96*coef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model3_day_season$chro.cl.99 <- coef_se_issp_2007_turnout_sleep_duration_model3_day_season$coef_chronotype_interval_beta - 2.576*coef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model3_day_season$chro.cu.99 <- coef_se_issp_2007_turnout_sleep_duration_model3_day_season$coef_chronotype_interval_beta + 2.576*coef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_chronotype_interval_beta

coef_se_issp_2007_turnout_sleep_duration_model3_day_season$coef_sleep_duration_beta <- round(coef_se_issp_2007_turnout_sleep_duration_model3_day_season$coef_sleep_duration_beta, 2)
coef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_sleep_duration_beta <- round(coef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_sleep_duration_beta, 2)
coef_se_issp_2007_turnout_sleep_duration_model3_day_season$coef_chronotype_interval_beta <- round(coef_se_issp_2007_turnout_sleep_duration_model3_day_season$coef_chronotype_interval_beta, 2)
coef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_chronotype_interval_beta <- round(coef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_chronotype_interval_beta, 2)
View(coef_se_issp_2007_turnout_sleep_duration_model3_day_season)

coef(issp_2007_turnout_sleep_duration_model4_day_season)
arm::se.coef(issp_2007_turnout_sleep_duration_model4_day_season)

coef(issp_2007_turnout_sleep_duration_model4_day_season)$country
coef_issp_2007_turnout_sleep_duration_model4_day_season <- data.frame(coef(issp_2007_turnout_sleep_duration_model4_day_season)$country)
coef_issp_2007_turnout_sleep_duration_model4_day_season <- subset(coef_issp_2007_turnout_sleep_duration_model4_day_season, select = c("sleep_duration_beta", "chronotype_interval_beta"))
coef_issp_2007_turnout_sleep_duration_model4_day_season$country <- row.names(coef_issp_2007_turnout_sleep_duration_model4_day_season)
names(coef_issp_2007_turnout_sleep_duration_model4_day_season)[names(coef_issp_2007_turnout_sleep_duration_model4_day_season)=="sleep_duration_beta"] <- "coef_sleep_duration_beta"
names(coef_issp_2007_turnout_sleep_duration_model4_day_season)[names(coef_issp_2007_turnout_sleep_duration_model4_day_season)=="chronotype_interval_beta"] <- "coef_chronotype_interval_beta"


se_coef_issp_2007_turnout_sleep_duration_model4_day_season <- data.frame(arm::se.coef(issp_2007_turnout_sleep_duration_model4_day_season)$country)
se_coef_issp_2007_turnout_sleep_duration_model4_day_season <- subset(se_coef_issp_2007_turnout_sleep_duration_model4_day_season, select = c("sleep_duration_beta", "chronotype_interval_beta"))
se_coef_issp_2007_turnout_sleep_duration_model4_day_season$country <- row.names(se_coef_issp_2007_turnout_sleep_duration_model4_day_season)
names(se_coef_issp_2007_turnout_sleep_duration_model4_day_season)[names(se_coef_issp_2007_turnout_sleep_duration_model4_day_season)=="sleep_duration_beta"] <- "se_sleep_duration_beta"
names(se_coef_issp_2007_turnout_sleep_duration_model4_day_season)[names(se_coef_issp_2007_turnout_sleep_duration_model4_day_season)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

coef_se_issp_2007_turnout_sleep_duration_model4_day_season <- merge(coef_issp_2007_turnout_sleep_duration_model4_day_season, se_coef_issp_2007_turnout_sleep_duration_model4_day_season, by="country")

coef_se_issp_2007_turnout_sleep_duration_model4_day_season$slp.dur.cl.90 <- coef_se_issp_2007_turnout_sleep_duration_model4_day_season$coef_sleep_duration_beta - 1.645*coef_se_issp_2007_turnout_sleep_duration_model4_day_season$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model4_day_season$slp.dur.cu.90 <- coef_se_issp_2007_turnout_sleep_duration_model4_day_season$coef_sleep_duration_beta + 1.645*coef_se_issp_2007_turnout_sleep_duration_model4_day_season$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model4_day_season$slp.dur.cl.95 <- coef_se_issp_2007_turnout_sleep_duration_model4_day_season$coef_sleep_duration_beta - 1.96*coef_se_issp_2007_turnout_sleep_duration_model4_day_season$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model4_day_season$slp.dur.cu.95 <- coef_se_issp_2007_turnout_sleep_duration_model4_day_season$coef_sleep_duration_beta + 1.96*coef_se_issp_2007_turnout_sleep_duration_model4_day_season$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model4_day_season$slp.dur.cl.99 <- coef_se_issp_2007_turnout_sleep_duration_model4_day_season$coef_sleep_duration_beta - 2.576*coef_se_issp_2007_turnout_sleep_duration_model4_day_season$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model4_day_season$slp.dur.cu.99 <- coef_se_issp_2007_turnout_sleep_duration_model4_day_season$coef_sleep_duration_beta + 2.576*coef_se_issp_2007_turnout_sleep_duration_model4_day_season$se_sleep_duration_beta

coef_se_issp_2007_turnout_sleep_duration_model4_day_season$chro.cl.90 <- coef_se_issp_2007_turnout_sleep_duration_model4_day_season$coef_chronotype_interval_beta - 1.645*coef_se_issp_2007_turnout_sleep_duration_model4_day_season$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model4_day_season$chro.cu.90 <- coef_se_issp_2007_turnout_sleep_duration_model4_day_season$coef_chronotype_interval_beta + 1.645*coef_se_issp_2007_turnout_sleep_duration_model4_day_season$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model4_day_season$chro.cl.95 <- coef_se_issp_2007_turnout_sleep_duration_model4_day_season$coef_chronotype_interval_beta - 1.96*coef_se_issp_2007_turnout_sleep_duration_model4_day_season$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model4_day_season$chro.cu.95 <- coef_se_issp_2007_turnout_sleep_duration_model4_day_season$coef_chronotype_interval_beta + 1.96*coef_se_issp_2007_turnout_sleep_duration_model4_day_season$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model4_day_season$chro.cl.99 <- coef_se_issp_2007_turnout_sleep_duration_model4_day_season$coef_chronotype_interval_beta - 2.576*coef_se_issp_2007_turnout_sleep_duration_model4_day_season$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model4_day_season$chro.cu.99 <- coef_se_issp_2007_turnout_sleep_duration_model4_day_season$coef_chronotype_interval_beta + 2.576*coef_se_issp_2007_turnout_sleep_duration_model4_day_season$se_chronotype_interval_beta

coef_se_issp_2007_turnout_sleep_duration_model4_day_season$coef_sleep_duration_beta <- round(coef_se_issp_2007_turnout_sleep_duration_model4_day_season$coef_sleep_duration_beta, 2)
coef_se_issp_2007_turnout_sleep_duration_model4_day_season$se_sleep_duration_beta <- round(coef_se_issp_2007_turnout_sleep_duration_model4_day_season$se_sleep_duration_beta, 2)
coef_se_issp_2007_turnout_sleep_duration_model4_day_season$coef_chronotype_interval_beta <- round(coef_se_issp_2007_turnout_sleep_duration_model4_day_season$coef_chronotype_interval_beta, 2)
coef_se_issp_2007_turnout_sleep_duration_model4_day_season$se_chronotype_interval_beta <- round(coef_se_issp_2007_turnout_sleep_duration_model4_day_season$se_chronotype_interval_beta, 2)
View(coef_se_issp_2007_turnout_sleep_duration_model4_day_season)

####  Online Appendices, Table 6 Random effects of sleep duration and chronotype on turnout for each country from the model additionally accounting for day and season grouping effect ####

ranef(issp_2007_turnout_sleep_duration_model1_day_season)
arm::se.ranef(issp_2007_turnout_sleep_duration_model1_day_season)

ranef(issp_2007_turnout_sleep_duration_model1_day_season)$country
ranef_issp_2007_turnout_sleep_duration_model1_day_season <- data.frame(ranef(issp_2007_turnout_sleep_duration_model1_day_season)$country)
ranef_issp_2007_turnout_sleep_duration_model1_day_season <- subset(ranef_issp_2007_turnout_sleep_duration_model1_day_season, select = c("sleep_duration_beta", "chronotype_interval_beta"))
ranef_issp_2007_turnout_sleep_duration_model1_day_season$country <- row.names(ranef_issp_2007_turnout_sleep_duration_model1_day_season)
names(ranef_issp_2007_turnout_sleep_duration_model1_day_season)[names(ranef_issp_2007_turnout_sleep_duration_model1_day_season)=="sleep_duration_beta"] <- "ranef_sleep_duration_beta"
names(ranef_issp_2007_turnout_sleep_duration_model1_day_season)[names(ranef_issp_2007_turnout_sleep_duration_model1_day_season)=="chronotype_interval_beta"] <- "ranef_chronotype_interval_beta"


se_ranef_issp_2007_turnout_sleep_duration_model1_day_season <- data.frame(arm::se.ranef(issp_2007_turnout_sleep_duration_model1_day_season)$country)
se_ranef_issp_2007_turnout_sleep_duration_model1_day_season <- subset(se_ranef_issp_2007_turnout_sleep_duration_model1_day_season, select = c("sleep_duration_beta", "chronotype_interval_beta"))
se_ranef_issp_2007_turnout_sleep_duration_model1_day_season$country <- row.names(se_ranef_issp_2007_turnout_sleep_duration_model1_day_season)
names(se_ranef_issp_2007_turnout_sleep_duration_model1_day_season)[names(se_ranef_issp_2007_turnout_sleep_duration_model1_day_season)=="sleep_duration_beta"] <- "se_sleep_duration_beta"
names(se_ranef_issp_2007_turnout_sleep_duration_model1_day_season)[names(se_ranef_issp_2007_turnout_sleep_duration_model1_day_season)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

ranef_se_issp_2007_turnout_sleep_duration_model1_day_season <- merge(ranef_issp_2007_turnout_sleep_duration_model1_day_season, se_ranef_issp_2007_turnout_sleep_duration_model1_day_season, by="country")

ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$slp.dur.cl.90 <- ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$ranef_sleep_duration_beta - 1.645*ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$slp.dur.cu.90 <- ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$ranef_sleep_duration_beta + 1.645*ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$slp.dur.cl.95 <- ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$ranef_sleep_duration_beta - 1.96*ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$slp.dur.cu.95 <- ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$ranef_sleep_duration_beta + 1.96*ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$slp.dur.cl.99 <- ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$ranef_sleep_duration_beta - 2.576*ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$slp.dur.cu.99 <- ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$ranef_sleep_duration_beta + 2.576*ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$se_sleep_duration_beta

ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$chro.cl.90 <- ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$ranef_chronotype_interval_beta - 1.645*ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$chro.cu.90 <- ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$ranef_chronotype_interval_beta + 1.645*ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$chro.cl.95 <- ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$ranef_chronotype_interval_beta - 1.96*ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$chro.cu.95 <- ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$ranef_chronotype_interval_beta + 1.96*ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$chro.cl.99 <- ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$ranef_chronotype_interval_beta - 2.576*ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$chro.cu.99 <- ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$ranef_chronotype_interval_beta + 2.576*ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$se_chronotype_interval_beta

ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$ranef_sleep_duration_beta <- round(ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$ranef_sleep_duration_beta, 3)
ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$se_sleep_duration_beta <- round(ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$se_sleep_duration_beta, 2)
ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$ranef_chronotype_interval_beta <- round(ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$ranef_chronotype_interval_beta, 2)
ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$se_chronotype_interval_beta <- round(ranef_se_issp_2007_turnout_sleep_duration_model1_day_season$se_chronotype_interval_beta, 2)
View(ranef_se_issp_2007_turnout_sleep_duration_model1_day_season)

ranef(issp_2007_turnout_sleep_duration_model2_day_season)
arm::se.ranef(issp_2007_turnout_sleep_duration_model2_day_season)

ranef(issp_2007_turnout_sleep_duration_model2_day_season)$country
ranef_issp_2007_turnout_sleep_duration_model2_day_season <- data.frame(ranef(issp_2007_turnout_sleep_duration_model2_day_season)$country)
ranef_issp_2007_turnout_sleep_duration_model2_day_season <- subset(ranef_issp_2007_turnout_sleep_duration_model2_day_season, select = c("sleep_duration_beta", "chronotype_interval_beta"))
ranef_issp_2007_turnout_sleep_duration_model2_day_season$country <- row.names(ranef_issp_2007_turnout_sleep_duration_model2_day_season)
names(ranef_issp_2007_turnout_sleep_duration_model2_day_season)[names(ranef_issp_2007_turnout_sleep_duration_model2_day_season)=="sleep_duration_beta"] <- "ranef_sleep_duration_beta"
names(ranef_issp_2007_turnout_sleep_duration_model2_day_season)[names(ranef_issp_2007_turnout_sleep_duration_model2_day_season)=="chronotype_interval_beta"] <- "ranef_chronotype_interval_beta"


se_ranef_issp_2007_turnout_sleep_duration_model2_day_season <- data.frame(arm::se.ranef(issp_2007_turnout_sleep_duration_model2_day_season)$country)
se_ranef_issp_2007_turnout_sleep_duration_model2_day_season <- subset(se_ranef_issp_2007_turnout_sleep_duration_model2_day_season, select = c("sleep_duration_beta", "chronotype_interval_beta"))
se_ranef_issp_2007_turnout_sleep_duration_model2_day_season$country <- row.names(se_ranef_issp_2007_turnout_sleep_duration_model2_day_season)
names(se_ranef_issp_2007_turnout_sleep_duration_model2_day_season)[names(se_ranef_issp_2007_turnout_sleep_duration_model2_day_season)=="sleep_duration_beta"] <- "se_sleep_duration_beta"
names(se_ranef_issp_2007_turnout_sleep_duration_model2_day_season)[names(se_ranef_issp_2007_turnout_sleep_duration_model2_day_season)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

ranef_se_issp_2007_turnout_sleep_duration_model2_day_season <- merge(ranef_issp_2007_turnout_sleep_duration_model2_day_season, se_ranef_issp_2007_turnout_sleep_duration_model2_day_season, by="country")

ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$slp.dur.cl.90 <- ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$ranef_sleep_duration_beta - 1.645*ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$slp.dur.cu.90 <- ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$ranef_sleep_duration_beta + 1.645*ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$slp.dur.cl.95 <- ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$ranef_sleep_duration_beta - 1.96*ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$slp.dur.cu.95 <- ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$ranef_sleep_duration_beta + 1.96*ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$slp.dur.cl.99 <- ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$ranef_sleep_duration_beta - 2.576*ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$slp.dur.cu.99 <- ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$ranef_sleep_duration_beta + 2.576*ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$se_sleep_duration_beta

ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$chro.cl.90 <- ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$ranef_chronotype_interval_beta - 1.645*ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$chro.cu.90 <- ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$ranef_chronotype_interval_beta + 1.645*ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$chro.cl.95 <- ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$ranef_chronotype_interval_beta - 1.96*ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$chro.cu.95 <- ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$ranef_chronotype_interval_beta + 1.96*ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$chro.cl.99 <- ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$ranef_chronotype_interval_beta - 2.576*ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$chro.cu.99 <- ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$ranef_chronotype_interval_beta + 2.576*ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$se_chronotype_interval_beta

ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$ranef_sleep_duration_beta <- round(ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$ranef_sleep_duration_beta, 3)
ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$se_sleep_duration_beta <- round(ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$se_sleep_duration_beta, 2)
ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$ranef_chronotype_interval_beta <- round(ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$ranef_chronotype_interval_beta, 3)
ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$se_chronotype_interval_beta <- round(ranef_se_issp_2007_turnout_sleep_duration_model2_day_season$se_chronotype_interval_beta, 2)
View(ranef_se_issp_2007_turnout_sleep_duration_model2_day_season)

ranef(issp_2007_turnout_sleep_duration_model3_day_season)
arm::se.ranef(issp_2007_turnout_sleep_duration_model3_day_season)

ranef(issp_2007_turnout_sleep_duration_model3_day_season)$country
ranef_issp_2007_turnout_sleep_duration_model3_day_season <- data.frame(ranef(issp_2007_turnout_sleep_duration_model3_day_season)$country)
ranef_issp_2007_turnout_sleep_duration_model3_day_season <- subset(ranef_issp_2007_turnout_sleep_duration_model3_day_season, select = c("sleep_duration_beta", "chronotype_interval_beta"))
ranef_issp_2007_turnout_sleep_duration_model3_day_season$country <- row.names(ranef_issp_2007_turnout_sleep_duration_model3_day_season)
names(ranef_issp_2007_turnout_sleep_duration_model3_day_season)[names(ranef_issp_2007_turnout_sleep_duration_model3_day_season)=="sleep_duration_beta"] <- "ranef_sleep_duration_beta"
names(ranef_issp_2007_turnout_sleep_duration_model3_day_season)[names(ranef_issp_2007_turnout_sleep_duration_model3_day_season)=="chronotype_interval_beta"] <- "ranef_chronotype_interval_beta"


se_ranef_issp_2007_turnout_sleep_duration_model3_day_season <- data.frame(arm::se.ranef(issp_2007_turnout_sleep_duration_model3_day_season)$country)
se_ranef_issp_2007_turnout_sleep_duration_model3_day_season <- subset(se_ranef_issp_2007_turnout_sleep_duration_model3_day_season, select = c("sleep_duration_beta", "chronotype_interval_beta"))
se_ranef_issp_2007_turnout_sleep_duration_model3_day_season$country <- row.names(se_ranef_issp_2007_turnout_sleep_duration_model3_day_season)
names(se_ranef_issp_2007_turnout_sleep_duration_model3_day_season)[names(se_ranef_issp_2007_turnout_sleep_duration_model3_day_season)=="sleep_duration_beta"] <- "se_sleep_duration_beta"
names(se_ranef_issp_2007_turnout_sleep_duration_model3_day_season)[names(se_ranef_issp_2007_turnout_sleep_duration_model3_day_season)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

ranef_se_issp_2007_turnout_sleep_duration_model3_day_season <- merge(ranef_issp_2007_turnout_sleep_duration_model3_day_season, se_ranef_issp_2007_turnout_sleep_duration_model3_day_season, by="country")

ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$slp.dur.cl.90 <- ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$ranef_sleep_duration_beta - 1.645*ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$slp.dur.cu.90 <- ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$ranef_sleep_duration_beta + 1.645*ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$slp.dur.cl.95 <- ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$ranef_sleep_duration_beta - 1.96*ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$slp.dur.cu.95 <- ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$ranef_sleep_duration_beta + 1.96*ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$slp.dur.cl.99 <- ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$ranef_sleep_duration_beta - 2.576*ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$slp.dur.cu.99 <- ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$ranef_sleep_duration_beta + 2.576*ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_sleep_duration_beta

ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$chro.cl.90 <- ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$ranef_chronotype_interval_beta - 1.645*ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$chro.cu.90 <- ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$ranef_chronotype_interval_beta + 1.645*ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$chro.cl.95 <- ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$ranef_chronotype_interval_beta - 1.96*ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$chro.cu.95 <- ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$ranef_chronotype_interval_beta + 1.96*ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$chro.cl.99 <- ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$ranef_chronotype_interval_beta - 2.576*ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$chro.cu.99 <- ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$ranef_chronotype_interval_beta + 2.576*ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_chronotype_interval_beta

ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$ranef_sleep_duration_beta <- round(ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$ranef_sleep_duration_beta, 2)
ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_sleep_duration_beta <- round(ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_sleep_duration_beta, 2)
ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$ranef_chronotype_interval_beta <- round(ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$ranef_chronotype_interval_beta, 2)
ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_chronotype_interval_beta <- round(ranef_se_issp_2007_turnout_sleep_duration_model3_day_season$se_chronotype_interval_beta, 2)
View(ranef_se_issp_2007_turnout_sleep_duration_model3_day_season)

ranef(issp_2007_turnout_sleep_duration_model4_day_season)
arm::se.ranef(issp_2007_turnout_sleep_duration_model4_day_season)

ranef(issp_2007_turnout_sleep_duration_model4_day_season)$country
ranef_issp_2007_turnout_sleep_duration_model4_day_season <- data.frame(ranef(issp_2007_turnout_sleep_duration_model4_day_season)$country)
ranef_issp_2007_turnout_sleep_duration_model4_day_season <- subset(ranef_issp_2007_turnout_sleep_duration_model4_day_season, select = c("sleep_duration_beta", "chronotype_interval_beta"))
ranef_issp_2007_turnout_sleep_duration_model4_day_season$country <- row.names(ranef_issp_2007_turnout_sleep_duration_model4_day_season)
names(ranef_issp_2007_turnout_sleep_duration_model4_day_season)[names(ranef_issp_2007_turnout_sleep_duration_model4_day_season)=="sleep_duration_beta"] <- "ranef_sleep_duration_beta"
names(ranef_issp_2007_turnout_sleep_duration_model4_day_season)[names(ranef_issp_2007_turnout_sleep_duration_model4_day_season)=="chronotype_interval_beta"] <- "ranef_chronotype_interval_beta"


se_ranef_issp_2007_turnout_sleep_duration_model4_day_season <- data.frame(arm::se.ranef(issp_2007_turnout_sleep_duration_model4_day_season)$country)
se_ranef_issp_2007_turnout_sleep_duration_model4_day_season <- subset(se_ranef_issp_2007_turnout_sleep_duration_model4_day_season, select = c("sleep_duration_beta", "chronotype_interval_beta"))
se_ranef_issp_2007_turnout_sleep_duration_model4_day_season$country <- row.names(se_ranef_issp_2007_turnout_sleep_duration_model4_day_season)
names(se_ranef_issp_2007_turnout_sleep_duration_model4_day_season)[names(se_ranef_issp_2007_turnout_sleep_duration_model4_day_season)=="sleep_duration_beta"] <- "se_sleep_duration_beta"
names(se_ranef_issp_2007_turnout_sleep_duration_model4_day_season)[names(se_ranef_issp_2007_turnout_sleep_duration_model4_day_season)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

ranef_se_issp_2007_turnout_sleep_duration_model4_day_season <- merge(ranef_issp_2007_turnout_sleep_duration_model4_day_season, se_ranef_issp_2007_turnout_sleep_duration_model4_day_season, by="country")

ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$slp.dur.cl.90 <- ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$ranef_sleep_duration_beta - 1.645*ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$slp.dur.cu.90 <- ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$ranef_sleep_duration_beta + 1.645*ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$slp.dur.cl.95 <- ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$ranef_sleep_duration_beta - 1.96*ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$slp.dur.cu.95 <- ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$ranef_sleep_duration_beta + 1.96*ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$slp.dur.cl.99 <- ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$ranef_sleep_duration_beta - 2.576*ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$slp.dur.cu.99 <- ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$ranef_sleep_duration_beta + 2.576*ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$se_sleep_duration_beta

ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$chro.cl.90 <- ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$ranef_chronotype_interval_beta - 1.645*ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$chro.cu.90 <- ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$ranef_chronotype_interval_beta + 1.645*ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$chro.cl.95 <- ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$ranef_chronotype_interval_beta - 1.96*ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$chro.cu.95 <- ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$ranef_chronotype_interval_beta + 1.96*ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$chro.cl.99 <- ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$ranef_chronotype_interval_beta - 2.576*ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$chro.cu.99 <- ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$ranef_chronotype_interval_beta + 2.576*ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$se_chronotype_interval_beta

ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$ranef_sleep_duration_beta <- round(ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$ranef_sleep_duration_beta, 3)
ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$se_sleep_duration_beta <- round(ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$se_sleep_duration_beta, 2)
ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$ranef_chronotype_interval_beta <- round(ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$ranef_chronotype_interval_beta, 2)
ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$se_chronotype_interval_beta <- round(ranef_se_issp_2007_turnout_sleep_duration_model4_day_season$se_chronotype_interval_beta, 2)
View(ranef_se_issp_2007_turnout_sleep_duration_model4_day_season)


#### Online Appendices, Table 7 Multilevel logistic regression models of turnout by sleep duration and chronotype additionally controlling for several individual and contextual covariates. ####

issp_2007_turnout_sleep_duration_model4_all <- glmer(turnout ~ sleep_duration_beta + chronotype_interval_beta + 
                                                       urban_rural_area_beta +
                                                       age_beta + degree_beta + income_beta + sex_beta + political_interest_beta + religiosity_beta +
                                                       religious_denomination_Buddhism_beta + 
                                                       religious_denomination_Christian_Orthodox_beta +
                                                       religious_denomination_Hinduism_beta +
                                                       religious_denomination_Islam_beta +
                                                       religious_denomination_Jewish_beta + 
                                                       religious_denomination_No_religion_beta +
                                                       religious_denomination_Other_Christian_Religions_beta +
                                                       religious_denomination_Other_Religions_beta +
                                                       religious_denomination_Protestant_beta + 
                                                       election_season_Summer_beta +
                                                       latitude_beta +
                                                       longitude_beta +
                                                       mean_temperature_beta +
                                                       polyarchy_beta + 
                                                       time_duration_election_turnout_beta +
                                                       (1  + sleep_duration_beta + chronotype_interval_beta | country/local_election) +
                                                       (1 + sleep_duration_beta + chronotype_interval_beta | mid_field_season/day_off), 
                                                     data =  issp_2007, family = "binomial", nAGQ=0)
summary(issp_2007_turnout_sleep_duration_model4_all)
tab_model(issp_2007_turnout_sleep_duration_model4_all, show.ci = 0.95)

stargazer(issp_2007_turnout_sleep_duration_model4_all,
          type = "html", title=" ", digits=2, out="Tables/all_controls_models_issp_2007_turnout_sleep_duration_chronotype.htm",
          model.numbers = F,
          column.labels = c("Model 1"),
          covariate.labels = c("Sleep Duration", 
                               "Chronotype",
                               "Urban-Rural Area of Residence", 
                               "Age", 
                               "Education", 
                               "Income", 
                               "Sex: Male (Base: Female)",
                               "Level on Interest in Politics", 
                               "Religious Attendance", 
                               "Religious Denomination: Buddhism",
                               "Religious Denomination: Christian Orthodox",
                               "Religious Denomination: Hinduism",
                               "Religious Denomination: Islam",
                               "Religious Denomination: Jewish",
                               "Religious Denomination: No Religion",
                               "Religious Denomination: Other Christian Religions",
                               "Religious Denomination: Other Religions",
                               "Religious Denomination: Protestant",
                               "Election Season: Summer (Base: Spring)",
                               "Latitude",
                               "Longitude",
                               "Mean Temperature",
                               "Electoral Democracy Index",
                               "Years since the Last Election",
                               "Intercept"))

####  Online Appendices, Table 8 Predicted effects of sleep duration and chronotype on turnout for each country from the model additionally controlling for several individual and contextual covariates. ####

coef(issp_2007_turnout_sleep_duration_model4_all)
arm::se.coef(issp_2007_turnout_sleep_duration_model4_all)

coef(issp_2007_turnout_sleep_duration_model4_all)$country
coef_issp_2007_turnout_sleep_duration_model4_all <- data.frame(coef(issp_2007_turnout_sleep_duration_model4_all)$country)
coef_issp_2007_turnout_sleep_duration_model4_all <- subset(coef_issp_2007_turnout_sleep_duration_model4_all, select = c("sleep_duration_beta", "chronotype_interval_beta"))
coef_issp_2007_turnout_sleep_duration_model4_all$country <- row.names(coef_issp_2007_turnout_sleep_duration_model4_all)
names(coef_issp_2007_turnout_sleep_duration_model4_all)[names(coef_issp_2007_turnout_sleep_duration_model4_all)=="sleep_duration_beta"] <- "coef_sleep_duration_beta"
names(coef_issp_2007_turnout_sleep_duration_model4_all)[names(coef_issp_2007_turnout_sleep_duration_model4_all)=="chronotype_interval_beta"] <- "coef_chronotype_interval_beta"


se_coef_issp_2007_turnout_sleep_duration_model4_all <- data.frame(arm::se.coef(issp_2007_turnout_sleep_duration_model4_all)$country)
se_coef_issp_2007_turnout_sleep_duration_model4_all <- subset(se_coef_issp_2007_turnout_sleep_duration_model4_all, select = c("sleep_duration_beta", "chronotype_interval_beta"))
se_coef_issp_2007_turnout_sleep_duration_model4_all$country <- row.names(se_coef_issp_2007_turnout_sleep_duration_model4_all)
names(se_coef_issp_2007_turnout_sleep_duration_model4_all)[names(se_coef_issp_2007_turnout_sleep_duration_model4_all)=="sleep_duration_beta"] <- "se_sleep_duration_beta"
names(se_coef_issp_2007_turnout_sleep_duration_model4_all)[names(se_coef_issp_2007_turnout_sleep_duration_model4_all)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

coef_se_issp_2007_turnout_sleep_duration_model4_all <- merge(coef_issp_2007_turnout_sleep_duration_model4_all, se_coef_issp_2007_turnout_sleep_duration_model4_all, by="country")

coef_se_issp_2007_turnout_sleep_duration_model4_all$slp.dur.cl.90 <- coef_se_issp_2007_turnout_sleep_duration_model4_all$coef_sleep_duration_beta - 1.645*coef_se_issp_2007_turnout_sleep_duration_model4_all$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model4_all$slp.dur.cu.90 <- coef_se_issp_2007_turnout_sleep_duration_model4_all$coef_sleep_duration_beta + 1.645*coef_se_issp_2007_turnout_sleep_duration_model4_all$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model4_all$slp.dur.cl.95 <- coef_se_issp_2007_turnout_sleep_duration_model4_all$coef_sleep_duration_beta - 1.96*coef_se_issp_2007_turnout_sleep_duration_model4_all$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model4_all$slp.dur.cu.95 <- coef_se_issp_2007_turnout_sleep_duration_model4_all$coef_sleep_duration_beta + 1.96*coef_se_issp_2007_turnout_sleep_duration_model4_all$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model4_all$slp.dur.cl.99 <- coef_se_issp_2007_turnout_sleep_duration_model4_all$coef_sleep_duration_beta - 2.576*coef_se_issp_2007_turnout_sleep_duration_model4_all$se_sleep_duration_beta
coef_se_issp_2007_turnout_sleep_duration_model4_all$slp.dur.cu.99 <- coef_se_issp_2007_turnout_sleep_duration_model4_all$coef_sleep_duration_beta + 2.576*coef_se_issp_2007_turnout_sleep_duration_model4_all$se_sleep_duration_beta

coef_se_issp_2007_turnout_sleep_duration_model4_all$chro.cl.90 <- coef_se_issp_2007_turnout_sleep_duration_model4_all$coef_chronotype_interval_beta - 1.645*coef_se_issp_2007_turnout_sleep_duration_model4_all$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model4_all$chro.cu.90 <- coef_se_issp_2007_turnout_sleep_duration_model4_all$coef_chronotype_interval_beta + 1.645*coef_se_issp_2007_turnout_sleep_duration_model4_all$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model4_all$chro.cl.95 <- coef_se_issp_2007_turnout_sleep_duration_model4_all$coef_chronotype_interval_beta - 1.96*coef_se_issp_2007_turnout_sleep_duration_model4_all$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model4_all$chro.cu.95 <- coef_se_issp_2007_turnout_sleep_duration_model4_all$coef_chronotype_interval_beta + 1.96*coef_se_issp_2007_turnout_sleep_duration_model4_all$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model4_all$chro.cl.99 <- coef_se_issp_2007_turnout_sleep_duration_model4_all$coef_chronotype_interval_beta - 2.576*coef_se_issp_2007_turnout_sleep_duration_model4_all$se_chronotype_interval_beta
coef_se_issp_2007_turnout_sleep_duration_model4_all$chro.cu.99 <- coef_se_issp_2007_turnout_sleep_duration_model4_all$coef_chronotype_interval_beta + 2.576*coef_se_issp_2007_turnout_sleep_duration_model4_all$se_chronotype_interval_beta

coef_se_issp_2007_turnout_sleep_duration_model4_all$coef_sleep_duration_beta <- round(coef_se_issp_2007_turnout_sleep_duration_model4_all$coef_sleep_duration_beta, 2)
coef_se_issp_2007_turnout_sleep_duration_model4_all$se_sleep_duration_beta <- round(coef_se_issp_2007_turnout_sleep_duration_model4_all$se_sleep_duration_beta, 2)
coef_se_issp_2007_turnout_sleep_duration_model4_all$coef_chronotype_interval_beta <- round(coef_se_issp_2007_turnout_sleep_duration_model4_all$coef_chronotype_interval_beta, 2)
coef_se_issp_2007_turnout_sleep_duration_model4_all$se_chronotype_interval_beta <- round(coef_se_issp_2007_turnout_sleep_duration_model4_all$se_chronotype_interval_beta, 2)
View(coef_se_issp_2007_turnout_sleep_duration_model4_all)

####  Online Appendices, Table 9 Random effects of sleep duration and chronotype on turnout for each country from the model additionally controlling for several individual and contextual covariates. ####

ranef(issp_2007_turnout_sleep_duration_model4_all)
arm::se.ranef(issp_2007_turnout_sleep_duration_model4_all)

ranef(issp_2007_turnout_sleep_duration_model4_all)$country
ranef_issp_2007_turnout_sleep_duration_model4_all <- data.frame(ranef(issp_2007_turnout_sleep_duration_model4_all)$country)
ranef_issp_2007_turnout_sleep_duration_model4_all <- subset(ranef_issp_2007_turnout_sleep_duration_model4_all, select = c("sleep_duration_beta", "chronotype_interval_beta"))
ranef_issp_2007_turnout_sleep_duration_model4_all$country <- row.names(ranef_issp_2007_turnout_sleep_duration_model4_all)
names(ranef_issp_2007_turnout_sleep_duration_model4_all)[names(ranef_issp_2007_turnout_sleep_duration_model4_all)=="sleep_duration_beta"] <- "ranef_sleep_duration_beta"
names(ranef_issp_2007_turnout_sleep_duration_model4_all)[names(ranef_issp_2007_turnout_sleep_duration_model4_all)=="chronotype_interval_beta"] <- "ranef_chronotype_interval_beta"


se_ranef_issp_2007_turnout_sleep_duration_model4_all <- data.frame(arm::se.ranef(issp_2007_turnout_sleep_duration_model4_all)$country)
se_ranef_issp_2007_turnout_sleep_duration_model4_all <- subset(se_ranef_issp_2007_turnout_sleep_duration_model4_all, select = c("sleep_duration_beta", "chronotype_interval_beta"))
se_ranef_issp_2007_turnout_sleep_duration_model4_all$country <- row.names(se_ranef_issp_2007_turnout_sleep_duration_model4_all)
names(se_ranef_issp_2007_turnout_sleep_duration_model4_all)[names(se_ranef_issp_2007_turnout_sleep_duration_model4_all)=="sleep_duration_beta"] <- "se_sleep_duration_beta"
names(se_ranef_issp_2007_turnout_sleep_duration_model4_all)[names(se_ranef_issp_2007_turnout_sleep_duration_model4_all)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

ranef_se_issp_2007_turnout_sleep_duration_model4_all <- merge(ranef_issp_2007_turnout_sleep_duration_model4_all, se_ranef_issp_2007_turnout_sleep_duration_model4_all, by="country")

ranef_se_issp_2007_turnout_sleep_duration_model4_all$slp.dur.cl.90 <- ranef_se_issp_2007_turnout_sleep_duration_model4_all$ranef_sleep_duration_beta - 1.645*ranef_se_issp_2007_turnout_sleep_duration_model4_all$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model4_all$slp.dur.cu.90 <- ranef_se_issp_2007_turnout_sleep_duration_model4_all$ranef_sleep_duration_beta + 1.645*ranef_se_issp_2007_turnout_sleep_duration_model4_all$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model4_all$slp.dur.cl.95 <- ranef_se_issp_2007_turnout_sleep_duration_model4_all$ranef_sleep_duration_beta - 1.96*ranef_se_issp_2007_turnout_sleep_duration_model4_all$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model4_all$slp.dur.cu.95 <- ranef_se_issp_2007_turnout_sleep_duration_model4_all$ranef_sleep_duration_beta + 1.96*ranef_se_issp_2007_turnout_sleep_duration_model4_all$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model4_all$slp.dur.cl.99 <- ranef_se_issp_2007_turnout_sleep_duration_model4_all$ranef_sleep_duration_beta - 2.576*ranef_se_issp_2007_turnout_sleep_duration_model4_all$se_sleep_duration_beta
ranef_se_issp_2007_turnout_sleep_duration_model4_all$slp.dur.cu.99 <- ranef_se_issp_2007_turnout_sleep_duration_model4_all$ranef_sleep_duration_beta + 2.576*ranef_se_issp_2007_turnout_sleep_duration_model4_all$se_sleep_duration_beta

ranef_se_issp_2007_turnout_sleep_duration_model4_all$chro.cl.90 <- ranef_se_issp_2007_turnout_sleep_duration_model4_all$ranef_chronotype_interval_beta - 1.645*ranef_se_issp_2007_turnout_sleep_duration_model4_all$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model4_all$chro.cu.90 <- ranef_se_issp_2007_turnout_sleep_duration_model4_all$ranef_chronotype_interval_beta + 1.645*ranef_se_issp_2007_turnout_sleep_duration_model4_all$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model4_all$chro.cl.95 <- ranef_se_issp_2007_turnout_sleep_duration_model4_all$ranef_chronotype_interval_beta - 1.96*ranef_se_issp_2007_turnout_sleep_duration_model4_all$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model4_all$chro.cu.95 <- ranef_se_issp_2007_turnout_sleep_duration_model4_all$ranef_chronotype_interval_beta + 1.96*ranef_se_issp_2007_turnout_sleep_duration_model4_all$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model4_all$chro.cl.99 <- ranef_se_issp_2007_turnout_sleep_duration_model4_all$ranef_chronotype_interval_beta - 2.576*ranef_se_issp_2007_turnout_sleep_duration_model4_all$se_chronotype_interval_beta
ranef_se_issp_2007_turnout_sleep_duration_model4_all$chro.cu.99 <- ranef_se_issp_2007_turnout_sleep_duration_model4_all$ranef_chronotype_interval_beta + 2.576*ranef_se_issp_2007_turnout_sleep_duration_model4_all$se_chronotype_interval_beta

ranef_se_issp_2007_turnout_sleep_duration_model4_all$ranef_sleep_duration_beta <- round(ranef_se_issp_2007_turnout_sleep_duration_model4_all$ranef_sleep_duration_beta, 2)
ranef_se_issp_2007_turnout_sleep_duration_model4_all$se_sleep_duration_beta <- round(ranef_se_issp_2007_turnout_sleep_duration_model4_all$se_sleep_duration_beta, 2)
ranef_se_issp_2007_turnout_sleep_duration_model4_all$ranef_chronotype_interval_beta <- round(ranef_se_issp_2007_turnout_sleep_duration_model4_all$ranef_chronotype_interval_beta, 2)
ranef_se_issp_2007_turnout_sleep_duration_model4_all$se_chronotype_interval_beta <- round(ranef_se_issp_2007_turnout_sleep_duration_model4_all$se_chronotype_interval_beta, 2)
View(ranef_se_issp_2007_turnout_sleep_duration_model4_all)

#### Online Appendices, Table 10 Multilevel logistic polynomial regression models of no turnout by sleep duration. ####

issp_2007$sleep_duration_power2 <- issp_2007$sleep_duration^2
hist(issp_2007$sleep_duration_power2)

issp_2007 <- issp_2007 %>%
  group_by(country) %>%
  mutate(group_mean_sleep_duration_power2 = mean(sleep_duration_power2, na.rm = T),
         group_sd_sleep_duration_power2 = sd(sleep_duration_power2, na.rm = T))

issp_2007$sleep_duration_power2_avg <- issp_2007$group_mean_sleep_duration_power2
issp_2007$sleep_duration_power2_sd <- issp_2007$group_sd_sleep_duration_power2
issp_2007$sleep_duration_power2_2sd <- issp_2007$sleep_duration_power2_sd*2
issp_2007$sleep_duration_power2_beta <- (issp_2007$sleep_duration_power2-issp_2007$sleep_duration_power2_avg)/issp_2007$sleep_duration_power2_2sd
hist(issp_2007$sleep_duration_power2_beta)

poly.issp.m1 <- glmer(turnout ~ sleep_duration_beta + sleep_duration_power2_beta + chronotype_interval_beta +
                        urban_rural_area_beta +
                        age_beta + degree_beta + income_beta + sex_beta + political_interest_beta + religiosity_beta +
                        (1  + sleep_duration_beta + sleep_duration_power2_beta + chronotype_interval_beta | country/local_election) +
                        (1 + sleep_duration_beta + sleep_duration_power2_beta + chronotype_interval_beta | mid_field_season/day_off), 
                      data =  issp_2007, family = "binomial", nAGQ=0,
                      control=glmerControl(optCtrl=list(maxfun=1e5)))
summary(poly.issp.m1)
tab_model(poly.issp.m1, show.ci = 0.95)

stargazer(poly.issp.m1,
          type = "html", title=" ", digits=2, out="Tables/models_polynomial.htm",
          model.numbers = F,
          column.labels = c("Model 1"),
          covariate.labels = c("Sleep Duration",
                               "Sleep Duration Squared",
                               "Chronotype",
                               "Urban-Rural Area of Residence", 
                               "Age", 
                               "Education", 
                               "Income", 
                               "Sex: Male (Base: Female)",
                               "Level on Interest in Politics", 
                               "Religious Attendance", 
                               "Intercept"))

#### Online Appendices, Table 11 Predicted effects of sleep duration and sleep duration squared on turnout for each country. ####

coef(poly.issp.m1)
arm::se.coef(poly.issp.m1)

coef(poly.issp.m1)$country
coef_poly.issp.m1 <- data.frame(coef(poly.issp.m1)$country)
coef_poly.issp.m1 <- subset(coef_poly.issp.m1, select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
coef_poly.issp.m1$country <- row.names(coef_poly.issp.m1)
names(coef_poly.issp.m1)[names(coef_poly.issp.m1)=="sleep_duration_beta"] <- "coef_sleep_duration_beta"
names(coef_poly.issp.m1)[names(coef_poly.issp.m1)=="sleep_duration_power2_beta"] <- "coef_sleep_duration_power2_beta"


se_coef_poly.issp.m1 <- data.frame(arm::se.coef(poly.issp.m1)$country)
se_coef_poly.issp.m1 <- subset(se_coef_poly.issp.m1, select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
se_coef_poly.issp.m1$country <- row.names(se_coef_poly.issp.m1)
names(se_coef_poly.issp.m1)[names(se_coef_poly.issp.m1)=="sleep_duration_beta"] <- "se_sleep_duration_beta"
names(se_coef_poly.issp.m1)[names(se_coef_poly.issp.m1)=="sleep_duration_power2_beta"] <- "se_sleep_duration_power2_beta"

coef_se_poly.issp.m1 <- merge(coef_poly.issp.m1, se_coef_poly.issp.m1, by="country")

coef_se_poly.issp.m1$slp.dur.cl.90 <- coef_se_poly.issp.m1$coef_sleep_duration_beta - 1.645*coef_se_poly.issp.m1$se_sleep_duration_beta
coef_se_poly.issp.m1$slp.dur.cu.90 <- coef_se_poly.issp.m1$coef_sleep_duration_beta + 1.645*coef_se_poly.issp.m1$se_sleep_duration_beta
coef_se_poly.issp.m1$slp.dur.cl.95 <- coef_se_poly.issp.m1$coef_sleep_duration_beta - 1.96*coef_se_poly.issp.m1$se_sleep_duration_beta
coef_se_poly.issp.m1$slp.dur.cu.95 <- coef_se_poly.issp.m1$coef_sleep_duration_beta + 1.96*coef_se_poly.issp.m1$se_sleep_duration_beta
coef_se_poly.issp.m1$slp.dur.cl.99 <- coef_se_poly.issp.m1$coef_sleep_duration_beta - 2.576*coef_se_poly.issp.m1$se_sleep_duration_beta
coef_se_poly.issp.m1$slp.dur.cu.99 <- coef_se_poly.issp.m1$coef_sleep_duration_beta + 2.576*coef_se_poly.issp.m1$se_sleep_duration_beta

coef_se_poly.issp.m1$slp.dur2.cl.90 <- coef_se_poly.issp.m1$coef_sleep_duration_power2_beta - 1.645*coef_se_poly.issp.m1$se_sleep_duration_power2_beta
coef_se_poly.issp.m1$slp.dur2.cu.90 <- coef_se_poly.issp.m1$coef_sleep_duration_power2_beta + 1.645*coef_se_poly.issp.m1$se_sleep_duration_power2_beta
coef_se_poly.issp.m1$slp.dur2.cl.95 <- coef_se_poly.issp.m1$coef_sleep_duration_power2_beta - 1.96*coef_se_poly.issp.m1$se_sleep_duration_power2_beta
coef_se_poly.issp.m1$slp.dur2.cu.95 <- coef_se_poly.issp.m1$coef_sleep_duration_power2_beta + 1.96*coef_se_poly.issp.m1$se_sleep_duration_power2_beta
coef_se_poly.issp.m1$slp.dur2.cl.99 <- coef_se_poly.issp.m1$coef_sleep_duration_power2_beta - 2.576*coef_se_poly.issp.m1$se_sleep_duration_power2_beta
coef_se_poly.issp.m1$slp.dur2.cu.99 <- coef_se_poly.issp.m1$coef_sleep_duration_power2_beta + 2.576*coef_se_poly.issp.m1$se_sleep_duration_power2_beta

coef_se_poly.issp.m1$coef_sleep_duration_beta <- round(coef_se_poly.issp.m1$coef_sleep_duration_beta, 2)
coef_se_poly.issp.m1$se_sleep_duration_beta <- round(coef_se_poly.issp.m1$se_sleep_duration_beta, 2)
coef_se_poly.issp.m1$coef_sleep_duration_power2_beta <- round(coef_se_poly.issp.m1$coef_sleep_duration_power2_beta, 2)
coef_se_poly.issp.m1$se_sleep_duration_power2_beta <- round(coef_se_poly.issp.m1$se_sleep_duration_power2_beta, 2)
View(coef_se_poly.issp.m1)

#### Online Appendices, Table 12 Random effects of sleep duration and sleep duration squared on turnout for each country. ####

ranef(poly.issp.m1)
arm::se.ranef(poly.issp.m1)

ranef(poly.issp.m1)$country
ranef_poly.issp.m1 <- data.frame(ranef(poly.issp.m1)$country)
ranef_poly.issp.m1 <- subset(ranef_poly.issp.m1, select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
ranef_poly.issp.m1$country <- row.names(ranef_poly.issp.m1)
names(ranef_poly.issp.m1)[names(ranef_poly.issp.m1)=="sleep_duration_beta"] <- "ranef_sleep_duration_beta"
names(ranef_poly.issp.m1)[names(ranef_poly.issp.m1)=="sleep_duration_power2_beta"] <- "ranef_sleep_duration_power2_beta"


se_ranef_poly.issp.m1 <- data.frame(arm::se.ranef(poly.issp.m1)$country)
se_ranef_poly.issp.m1 <- subset(se_ranef_poly.issp.m1, select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
se_ranef_poly.issp.m1$country <- row.names(se_ranef_poly.issp.m1)
names(se_ranef_poly.issp.m1)[names(se_ranef_poly.issp.m1)=="sleep_duration_beta"] <- "se_sleep_duration_beta"
names(se_ranef_poly.issp.m1)[names(se_ranef_poly.issp.m1)=="sleep_duration_power2_beta"] <- "se_sleep_duration_power2_beta"

ranef_se_poly.issp.m1 <- merge(ranef_poly.issp.m1, se_ranef_poly.issp.m1, by="country")

ranef_se_poly.issp.m1$slp.dur.cl.90 <- ranef_se_poly.issp.m1$ranef_sleep_duration_beta - 1.645*ranef_se_poly.issp.m1$se_sleep_duration_beta
ranef_se_poly.issp.m1$slp.dur.cu.90 <- ranef_se_poly.issp.m1$ranef_sleep_duration_beta + 1.645*ranef_se_poly.issp.m1$se_sleep_duration_beta
ranef_se_poly.issp.m1$slp.dur.cl.95 <- ranef_se_poly.issp.m1$ranef_sleep_duration_beta - 1.96*ranef_se_poly.issp.m1$se_sleep_duration_beta
ranef_se_poly.issp.m1$slp.dur.cu.95 <- ranef_se_poly.issp.m1$ranef_sleep_duration_beta + 1.96*ranef_se_poly.issp.m1$se_sleep_duration_beta
ranef_se_poly.issp.m1$slp.dur.cl.99 <- ranef_se_poly.issp.m1$ranef_sleep_duration_beta - 2.576*ranef_se_poly.issp.m1$se_sleep_duration_beta
ranef_se_poly.issp.m1$slp.dur.cu.99 <- ranef_se_poly.issp.m1$ranef_sleep_duration_beta + 2.576*ranef_se_poly.issp.m1$se_sleep_duration_beta

ranef_se_poly.issp.m1$slp.dur2.cl.90 <- ranef_se_poly.issp.m1$ranef_sleep_duration_power2_beta - 1.645*ranef_se_poly.issp.m1$se_sleep_duration_power2_beta
ranef_se_poly.issp.m1$slp.dur2.cu.90 <- ranef_se_poly.issp.m1$ranef_sleep_duration_power2_beta + 1.645*ranef_se_poly.issp.m1$se_sleep_duration_power2_beta
ranef_se_poly.issp.m1$slp.dur2.cl.95 <- ranef_se_poly.issp.m1$ranef_sleep_duration_power2_beta - 1.96*ranef_se_poly.issp.m1$se_sleep_duration_power2_beta
ranef_se_poly.issp.m1$slp.dur2.cu.95 <- ranef_se_poly.issp.m1$ranef_sleep_duration_power2_beta + 1.96*ranef_se_poly.issp.m1$se_sleep_duration_power2_beta
ranef_se_poly.issp.m1$slp.dur2.cl.99 <- ranef_se_poly.issp.m1$ranef_sleep_duration_power2_beta - 2.576*ranef_se_poly.issp.m1$se_sleep_duration_power2_beta
ranef_se_poly.issp.m1$slp.dur2.cu.99 <- ranef_se_poly.issp.m1$ranef_sleep_duration_power2_beta + 2.576*ranef_se_poly.issp.m1$se_sleep_duration_power2_beta

ranef_se_poly.issp.m1$ranef_sleep_duration_beta <- round(ranef_se_poly.issp.m1$ranef_sleep_duration_beta, 2)
ranef_se_poly.issp.m1$se_sleep_duration_beta <- round(ranef_se_poly.issp.m1$se_sleep_duration_beta, 2)
ranef_se_poly.issp.m1$ranef_chronotype_interval_beta <- round(ranef_se_poly.issp.m1$ranef_sleep_duration_power2_beta, 2)
ranef_se_poly.issp.m1$se_chronotype_interval_beta <- round(ranef_se_poly.issp.m1$se_sleep_duration_power2_beta, 2)

View(ranef_se_poly.issp.m1)

#### Manuscript, Fig. 2. Fitted values of turnout by sleep duration and sleep duration squared. ####

number_ticks <- function(n) {function(limits) pretty(limits, n)}

table(issp_2007$country, exclude = NULL)

issp_2007$predicted_finland <- predict(poly.issp.m1, 
                                       newdata=transform(issp_2007, 
                                                         urban_rural_area_beta = 0,
                                                         age_beta  = 0, 
                                                         degree_beta  = 0,
                                                         income_beta   = 0,
                                                         sex_beta   = 0,
                                                         political_interest_beta   = 0,
                                                         religiosity_beta = 0,
                                                         country="Finland",
                                                         local_election=0,
                                                         mid_field_season=0,
                                                         day_off=0), type = "response", allow.new.levels=T)

finland <- subset(issp_2007, subset = country == "Finland", select = c("sleep_duration", "predicted_finland"))

describe(finland$predicted_finland)
table(finland$predicted_finland)
describe(finland$sleep_duration)

finland_plot <- ggplot(finland, aes(x=sleep_duration, y=predicted_finland)) + 
  geom_point()  +
  stat_smooth(method='lm', formula = y ~ x, size = 0.5, level = 0.95, fullrange = TRUE, color = "black", linetype="longdash") +
  stat_smooth(method='lm', formula = y ~ poly(x,2), size = 0.5, level = 0.95, fullrange = TRUE, color = "black") +
  theme_classic() + ggpubr::grids(linetype = "dashed") +
  theme(axis.text.x=element_text(colour="black", size = 12), axis.text.y=element_text(colour="black", size = 12)) +
  theme(axis.title=element_text(size=14, colour="black", face="bold"), legend.text=element_text(size=14), legend.title=element_text(size=14)) +
  scale_x_continuous(breaks=c(2.17, 3.65, 5.15, 6.65, 8.15, 9.65, 11.15, 12.65, 15),
                     labels = c("2.17\n(Min.)", "3.65\n(μ-3SD)", "5.15\n(μ-2SD)", "6.65\n(μ-1SD)", "8.15\n(μ)", "9.65\n(μ+1SD)", "11.15\n(μ+2SD)", "12.65\n(μ+3SD)", "15\n(Max.)")) + scale_y_continuous(breaks=number_ticks(10)) +
  scale_y_continuous(labels = function(x) paste0(x * 100, '%'), limits = c(0.72, 0.90), breaks = seq(0.72,0.90, by = .03)) +
  labs(    x = "\n Sleep Duration (in Hours)\n", 
           y = "\n Fitted Values of Turnout \n",
           title = "")
finland_plot

finland_plot <- ggExtra::ggMarginal(finland_plot, type = "boxplot")

finland_plot

issp_2007$predicted_greece <- predict(poly.issp.m1, 
                                      newdata=transform(issp_2007, 
                                                        urban_rural_area_beta = 0,
                                                        age_beta  = 0, 
                                                        degree_beta  = 0,
                                                        income_beta   = 0,
                                                        sex_beta   = 0,
                                                        political_interest_beta   = 0,
                                                        religiosity_beta = 0,
                                                        country="Greece",
                                                        local_election=0,
                                                        mid_field_season=0,
                                                        day_off=0), type = "response", allow.new.levels=T)

greece <- subset(issp_2007, subset = country == "Greece", select = c("sleep_duration", "predicted_greece"))

describe(greece$predicted_greece)
table(greece$predicted_greece)
describe(greece$sleep_duration)

greece_plot <- ggplot(greece, aes(x=sleep_duration, y=predicted_greece)) + 
  geom_point()  +
  stat_smooth(method='lm', formula = y ~ x, size = 0.5, level = 0.95, fullrange = TRUE, color = "black", linetype="longdash") +
  stat_smooth(method='lm', formula = y ~ poly(x,2), size = 0.5, level = 0.95, fullrange = TRUE, color = "black") +
  theme_classic() + ggpubr::grids(linetype = "dashed") +
  theme(axis.text.x=element_text(colour="black", size = 12), axis.text.y=element_text(colour="black", size = 12)) +
  theme(axis.title=element_text(size=14, colour="black", face="bold"), legend.text=element_text(size=14), legend.title=element_text(size=14)) +
  scale_x_continuous(breaks=c(4, 5.36, 6.63, 7.9, 9.17, 10.44, 12.25),
                     labels = c("4\n(Min.)", "5.36\n(μ-2SD)", "6.63\n(μ-1SD)", "7.9\n(μ)", "9.17\n(μ+1SD)", "10.44\n(μ+2SD)", "12.25\n(Max.)")) + scale_y_continuous(breaks=number_ticks(10)) +
  scale_y_continuous(labels = function(x) paste0(x * 100, '%'), limits = c(0.87, 0.92), breaks = seq(0.87, 0.92, by = .01)) +
  labs(    x = "\n Sleep Duration (in Hours)\n", 
           y = "\n Fitted Values of Turnout \n",
           title = "")
greece_plot

greece_plot <- ggExtra::ggMarginal(greece_plot, type = "boxplot")

greece_plot

issp_2007$predicted_ireland <- predict(poly.issp.m1, 
                                       newdata=transform(issp_2007, 
                                                         urban_rural_area_beta = 0,
                                                         age_beta  = 0, 
                                                         degree_beta  = 0,
                                                         income_beta   = 0,
                                                         sex_beta   = 0,
                                                         political_interest_beta   = 0,
                                                         religiosity_beta = 0,
                                                         country="Ireland",
                                                         local_election=0,
                                                         mid_field_season=0,
                                                         day_off=0), type = "response", allow.new.levels=T)

ireland <- subset(issp_2007, subset = country == "Ireland", select = c("sleep_duration", "predicted_ireland"))

describe(ireland$predicted_ireland)
table(ireland$predicted_ireland)
describe(ireland$sleep_duration)

ireland_plot <- ggplot(ireland, aes(x=sleep_duration, y=predicted_ireland)) + 
  geom_point()  +
  stat_smooth(method='lm', formula = y ~ x, size = 0.5, level = 0.95, fullrange = TRUE, color = "black", linetype="longdash") +
  stat_smooth(method='lm', formula = y ~ poly(x,2), size = 0.5, level = 0.95, fullrange = TRUE, color = "black") +
  theme_classic() + ggpubr::grids(linetype = "dashed") +
  theme(axis.text.x=element_text(colour="black", size = 12), axis.text.y=element_text(colour="black", size = 12)) +
  theme(axis.title=element_text(size=14, colour="black", face="bold"), legend.text=element_text(size=14), legend.title=element_text(size=14)) +
  scale_x_continuous(breaks=c(2, 3.73, 5.29, 6.85, 8.41, 9.97, 11.53, 13.09, 15.5),
                     labels = c("2\n(Min.)", "3.73\n(μ-3SD)", "5.29\n(μ-2SD)", "6.85\n(μ-1SD)", "8.41\n(μ)", "9.97\n(μ+1SD)", "11.53\n(μ+2SD)", "13.09\n(μ+3SD)", "15.5\n(Max.)")) + scale_y_continuous(breaks=number_ticks(10)) +
  scale_y_continuous(labels = function(x) paste0(x * 100, '%'), limits = c(0.325, 0.825), breaks = seq(0.325,0.825, by = .05)) +
  labs(    x = "\n Sleep Duration (in Hours)\n", 
           y = "\n Fitted Values of Turnout \n",
           title = "")
ireland_plot

ireland_plot <- ggExtra::ggMarginal(ireland_plot, type = "boxplot")

ireland_plot

issp_2007$predicted_mexico <- predict(poly.issp.m1, 
                                      newdata=transform(issp_2007, 
                                                        urban_rural_area_beta = 0,
                                                        age_beta  = 0, 
                                                        degree_beta  = 0,
                                                        income_beta   = 0,
                                                        sex_beta   = 0,
                                                        political_interest_beta   = 0,
                                                        religiosity_beta = 0,
                                                        country="Mexico",
                                                        local_election=0,
                                                        mid_field_season=0,
                                                        day_off=0), type = "response", allow.new.levels=T)

mexico <- subset(issp_2007, subset = country == "Mexico", select = c("sleep_duration", "predicted_mexico"))

describe(mexico$predicted_mexico)
table(mexico$predicted_mexico)
describe(mexico$sleep_duration)

mexico_plot <- ggplot(mexico, aes(x=sleep_duration, y=predicted_mexico)) + 
  geom_point()  +
  stat_smooth(method='lm', formula = y ~ x, size = 0.5, level = 0.95, fullrange = TRUE, color = "black", linetype="longdash") +
  stat_smooth(method='lm', formula = y ~ poly(x,2), size = 0.5, level = 0.95, fullrange = TRUE, color = "black") +
  theme_classic() + ggpubr::grids(linetype = "dashed") +
  theme(axis.text.x=element_text(colour="black", size = 12), axis.text.y=element_text(colour="black", size = 12)) +
  theme(axis.title=element_text(size=14, colour="black", face="bold"), legend.text=element_text(size=14), legend.title=element_text(size=14)) +
  scale_x_continuous(breaks=c(2, 4.48, 6.25, 8.02, 9.79, 11.56, 13.33, 16),
                     labels = c("2\n(Min.)", "4.48\n(μ-2SD)", "6.25\n(μ-1SD)", "8.02\n(μ)", "9.79\n(μ+1SD)", "11.56\n(μ+2SD)", "13.33\n(μ+3SD)", "16\n(Max.)")) + scale_y_continuous(breaks=number_ticks(10)) +
  scale_y_continuous(labels = function(x) paste0(x * 100, '%'), limits = c(0.6, 0.9), breaks = seq(0.6,0.9, by = .05)) +
  labs(    x = "\n Sleep Duration (in Hours)\n", 
           y = "\n Fitted Values of Turnout \n",
           title = "")
mexico_plot

mexico_plot <- ggExtra::ggMarginal(mexico_plot, type = "boxplot")

mexico_plot

issp_2007$predicted_new_zealand <- predict(poly.issp.m1, 
                                           newdata=transform(issp_2007, 
                                                             urban_rural_area_beta = 0,
                                                             age_beta  = 0, 
                                                             degree_beta  = 0,
                                                             income_beta   = 0,
                                                             sex_beta   = 0,
                                                             political_interest_beta   = 0,
                                                             religiosity_beta = 0,
                                                             country="New Zealand",
                                                             local_election=0,
                                                             mid_field_season=0,
                                                             day_off=0), type = "response", allow.new.levels=T)

new_zealand <- subset(issp_2007, subset = country == "New Zealand", select = c("sleep_duration", "predicted_new_zealand"))

describe(new_zealand$predicted_new_zealand)
table(new_zealand$predicted_new_zealand)
describe(new_zealand$sleep_duration)

new_zealand_plot <- ggplot(new_zealand, aes(x=sleep_duration, y=predicted_new_zealand)) + 
  geom_point()  +
  stat_smooth(method='lm', formula = y ~ x, size = 0.5, level = 0.95, fullrange = TRUE, color = "black", linetype="longdash") +
  stat_smooth(method='lm', formula = y ~ poly(x,2), size = 0.5, level = 0.95, fullrange = TRUE, color = "black") +
  theme_classic() + ggpubr::grids(linetype = "dashed") +
  theme(axis.text.x=element_text(colour="black", size = 12), axis.text.y=element_text(colour="black", size = 12)) +
  theme(axis.title=element_text(size=14, colour="black", face="bold"), legend.text=element_text(size=14), legend.title=element_text(size=14)) +
  scale_x_continuous(breaks=c(2.5, 4.07, 5.57, 7.07, 8.57, 10.07, 11.57, 14),
                     labels = c("2.5\n(Min.)", "4.07\n(μ-3SD)", "5.57\n(μ-2SD)", "7.07\n(μ-1SD)", "8.57\n(μ)", "10.07\n(μ+1SD)", "11.57\n(μ+2SD)" , "14\n(Max.)")) + scale_y_continuous(breaks=number_ticks(10)) +
  scale_y_continuous(labels = function(x) paste0(x * 100, '%'), limits = c(0.885, 0.935), breaks = seq(0.885, 0.935, by = 0.01)) +
  labs(    x = "\n Sleep Duration (in Hours)\n", 
           y = "\n Fitted Values of Turnout \n",
           title = "")
new_zealand_plot

new_zealand_plot <- ggExtra::ggMarginal(new_zealand_plot, type = "boxplot")

new_zealand_plot

issp_2007$predicted_philippines <- predict(poly.issp.m1, 
                                           newdata=transform(issp_2007, 
                                                             urban_rural_area_beta = 0,
                                                             age_beta  = 0, 
                                                             degree_beta  = 0,
                                                             income_beta   = 0,
                                                             sex_beta   = 0,
                                                             political_interest_beta   = 0,
                                                             religiosity_beta = 0,
                                                             country="the Philippines",
                                                             local_election=0,
                                                             mid_field_season=0,
                                                             day_off=0), type = "response", allow.new.levels=T)

philippines <- subset(issp_2007, subset = country == "the Philippines", select = c("sleep_duration", "predicted_philippines"))

describe(philippines$predicted_philippines)
table(philippines$predicted_philippines)
describe(philippines$sleep_duration)

philippines_plot <- ggplot(philippines, aes(x=sleep_duration, y=predicted_philippines)) + 
  geom_point()  +
  stat_smooth(method='lm', formula = y ~ x, size = 0.5, level = 0.95, fullrange = TRUE, color = "black", linetype="longdash") +
  stat_smooth(method='lm', formula = y ~ poly(x,2), size = 0.5, level = 0.95, fullrange = TRUE, color = "black") +
  theme_classic() + ggpubr::grids(linetype = "dashed") +
  theme(axis.text.x=element_text(colour="black", size = 12), axis.text.y=element_text(colour="black", size = 12)) +
  theme(axis.title=element_text(size=14, colour="black", face="bold"), legend.text=element_text(size=14), legend.title=element_text(size=14)) +
  scale_x_continuous(breaks=c(2, 4.52, 6.39, 8.26, 10.13, 12, 15),
                     labels = c("2\n(Min.)", "4.52\n(μ-2SD)", "6.39\n(μ-1SD)", "8.26\n(μ)", "10.13\n(μ+1SD)", "12\n(μ+2SD)",  "15\n(Max.)")) + scale_y_continuous(breaks=number_ticks(10)) +
  scale_y_continuous(labels = function(x) paste0(x * 100, '%'), limits = c(0.68, 0.88), breaks = seq(0.68,0.88, by = .04)) +
  labs(    x = "\n Sleep Duration (in Hours)\n", 
           y = "\n Fitted Values of Turnout \n",
           title = "")
philippines_plot

philippines_plot <- ggExtra::ggMarginal(philippines_plot, type = "boxplot")

philippines_plot

issp_2007$predicted_south_korea <- predict(poly.issp.m1, 
                                           newdata=transform(issp_2007, 
                                                             urban_rural_area_beta = 0,
                                                             age_beta  = 0, 
                                                             degree_beta  = 0,
                                                             income_beta   = 0,
                                                             sex_beta   = 0,
                                                             political_interest_beta   = 0,
                                                             religiosity_beta = 0,
                                                             country="South Korea",
                                                             local_election=0,
                                                             mid_field_season=0,
                                                             day_off=0), type = "response", allow.new.levels=T)

south_korea <- subset(issp_2007, subset = country == "South Korea", select = c("sleep_duration", "predicted_south_korea"))

describe(south_korea$predicted_south_korea)
table(south_korea$predicted_south_korea)
describe(south_korea$sleep_duration)

south_korea_plot <- ggplot(south_korea, aes(x=sleep_duration, y=predicted_south_korea)) + 
  geom_point()  +
  stat_smooth(method='lm', formula = y ~ x, size = 0.5, level = 0.95, fullrange = TRUE, color = "black", linetype="longdash") +
  stat_smooth(method='lm', formula = y ~ poly(x,2), size = 0.5, level = 0.95, fullrange = TRUE, color = "black") +
  theme_classic() + ggpubr::grids(linetype = "dashed") +
  theme(axis.text.x=element_text(colour="black", size = 12), axis.text.y=element_text(colour="black", size = 12)) +
  theme(axis.title=element_text(size=14, colour="black", face="bold"), legend.text=element_text(size=14), legend.title=element_text(size=14)) +
  scale_x_continuous(breaks=c(2, 3.61, 5.27, 6.93, 8.59, 10.25, 11.91, 13.57, 16),
                     labels = c("2\n(Min.)", "3.61\n(μ-2SD)", "5.27\n(μ-1SD)", "6.93\n(μ)", "8.59\n(μ+1SD)", "10.25\n(μ+2SD)","11.91\n(μ+3SD)", "13.57\n(μ+4SD)",  "16\n(Max.)")) + scale_y_continuous(breaks=number_ticks(10)) +
  scale_y_continuous(labels = function(x) paste0(x * 100, '%'), limits = c(0.70, 0.90), breaks = seq(0.70, 0.90, by = 0.05)) +
  labs(    x = "\n Sleep Duration (in Hours)\n", 
           y = "\n Fitted Values of Turnout \n",
           title = "")
south_korea_plot

south_korea_plot <- ggExtra::ggMarginal(south_korea_plot, type = "boxplot")

south_korea_plot

issp_2007$predicted_netherlands <- predict(poly.issp.m1, 
                                           newdata=transform(issp_2007, 
                                                             urban_rural_area_beta = 0,
                                                             age_beta  = 0, 
                                                             degree_beta  = 0,
                                                             income_beta   = 0,
                                                             sex_beta   = 0,
                                                             political_interest_beta   = 0,
                                                             religiosity_beta = 0,
                                                             country="the Netherlands",
                                                             local_election=0,
                                                             mid_field_season=0,
                                                             day_off=0), type = "response", allow.new.levels=T)

netherlands <- subset(issp_2007, subset = country == "the Netherlands", select = c("sleep_duration", "predicted_netherlands"))

describe(netherlands$predicted_netherlands)
table(netherlands$predicted_netherlands)
describe(netherlands$sleep_duration)

netherlands_plot <- ggplot(netherlands, aes(x=sleep_duration, y=predicted_netherlands)) + 
  geom_point()  +
  stat_smooth(method='lm', formula = y ~ x, size = 0.5, level = 0.95, fullrange = TRUE, color = "black", linetype="longdash") +
  stat_smooth(method='lm', formula = y ~ poly(x,2), size = 0.5, level = 0.95, fullrange = TRUE, color = "black") +
  theme_classic() + ggpubr::grids(linetype = "dashed") +
  theme(axis.text.x=element_text(colour="black", size = 12), axis.text.y=element_text(colour="black", size = 12)) +
  theme(axis.title=element_text(size=14, colour="black", face="bold"), legend.text=element_text(size=14), legend.title=element_text(size=14)) +
  scale_x_continuous(breaks=c(3, 5.13, 6.73, 8.33, 9.93, 11.53, 13.13, 15),
                     labels = c("3\n(Min.)", "5.13\n(μ-2SD)", "6.73\n(μ-1SD)", "8.33\n(μ)", "9.93\n(μ+1SD)", "11.53\n(μ+2SD)","13.13\n(μ+3SD)",  "15\n(Max.)")) + scale_y_continuous(breaks=number_ticks(10)) +
  scale_y_continuous(labels = function(x) paste0(x * 100, '%'), limits = c(0.80, 0.95), breaks = seq(0.80, 0.95, by = 0.025)) +
  labs(    x = "\n Sleep Duration (in Hours)\n", 
           y = "\n Fitted Values of Turnout \n",
           title = "")
netherlands_plot

netherlands_plot <- ggExtra::ggMarginal(netherlands_plot, type = "boxplot")

netherlands_plot

issp_2007$predicted_russia <- predict(poly.issp.m1, 
                                      newdata=transform(issp_2007, 
                                                        urban_rural_area_beta = 0,
                                                        age_beta  = 0, 
                                                        degree_beta  = 0,
                                                        income_beta   = 0,
                                                        sex_beta   = 0,
                                                        political_interest_beta   = 0,
                                                        religiosity_beta = 0,
                                                        country="Russia",
                                                        local_election=0,
                                                        mid_field_season=0,
                                                        day_off=0), type = "response", allow.new.levels=T)

russia <- subset(issp_2007, subset = country == "Russia", select = c("sleep_duration", "predicted_russia"))

describe(russia$predicted_russia)
table(russia$predicted_russia)
describe(russia$sleep_duration)

russia_plot <- ggplot(russia, aes(x=sleep_duration, y=predicted_russia)) + 
  geom_point()  +
  stat_smooth(method='lm', formula = y ~ x, size = 0.5, level = 0.95, fullrange = TRUE, color = "black", linetype="longdash") +
  stat_smooth(method='lm', formula = y ~ poly(x,2), size = 0.5, level = 0.95, fullrange = TRUE, color = "black") +
  theme_classic() + ggpubr::grids(linetype = "dashed") +
  theme(axis.text.x=element_text(colour="black", size = 12), axis.text.y=element_text(colour="black", size = 12)) +
  theme(axis.title=element_text(size=14, colour="black", face="bold"), legend.text=element_text(size=14), legend.title=element_text(size=14)) +
  scale_x_continuous(breaks=c(2, 4.32, 6.12, 7.92, 9.72, 11.52, 14),
                     labels = c("2\n(Min.)", "4.32\n(μ-2SD)", "6.12\n(μ-1SD)", "7.92\n(μ)", "9.72\n(μ+1SD)", "11.52\n(μ+2SD)", "14\n(Max.)")) + scale_y_continuous(breaks=number_ticks(10)) +
  scale_y_continuous(labels = function(x) paste0(x * 100, '%'), limits = c(0.95, 1), breaks = seq(0.95, 1, by = 0.005)) +
  labs(    x = "\n Sleep Duration (in Hours)\n", 
           y = "\n Fitted Values of Turnout \n",
           title = "")
russia_plot

russia_plot <- ggExtra::ggMarginal(russia_plot, type = "boxplot")

russia_plot

poly_figures <- ggpubr::ggarrange(finland_plot,  greece_plot, 
                                  ireland_plot, mexico_plot, netherlands_plot, new_zealand_plot,
                                  philippines_plot, south_korea_plot, russia_plot,
                                  ncol=3, nrow = 3, labels = c("A: Finland", "B: Greece",
                                                               "C: Ireland", "D: Mexico", 
                                                               "E: the Netherlands", 
                                                               "F: New Zealand",
                                                               "G: the Philippines", "H: South Korea",
                                                               "I: Russia"))
poly_figures

ggsave(filename = "poly_figures.jpg",plot = poly_figures, width = 28, height = 16, dpi = 1000)

#### Manuscript, Fig. 3. Differences in the predicted probabilities of turnout compared to the peak sleep duration hours. ####

subset(issp_2007, subset = sleep_duration == 3 & country == "Ireland", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 4 & country == "Ireland", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 5 & country == "Ireland", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 6 & country == "Ireland", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 7 & country == "Ireland", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 8 & country == "Ireland", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 9 & country == "Ireland", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 10 & country == "Ireland", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 11 & country == "Ireland", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))



issp_2007$IE.PRED.3 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -1.73,
                                                                 sleep_duration_power2_beta =  -1.19,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="Ireland",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$IE.PRED.4 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -1.41,
                                                                 sleep_duration_power2_beta = -1.06,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="Ireland",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$IE.PRED.5 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -1.09,
                                                                 sleep_duration_power2_beta = -0.897,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="Ireland",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$IE.PRED.6 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -0.770,
                                                                 sleep_duration_power2_beta = -0.692,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="Ireland",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$IE.PRED.7 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -0.451,
                                                                 sleep_duration_power2_beta = -0.450,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="Ireland",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$IE.PRED.8 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -0.131,
                                                                 sleep_duration_power2_beta = -0.171,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="Ireland",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$IE.PRED.9 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = 0.188,
                                                                 sleep_duration_power2_beta = 0.145,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="Ireland",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$IE.PRED.10 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                  sleep_duration_beta = 0.508,
                                                                  sleep_duration_power2_beta = 0.499,
                                                                  urban_rural_area_beta = 0,
                                                                  age_beta  = 0, 
                                                                  degree_beta  = 0,
                                                                  income_beta   = 0,
                                                                  sex_beta   = 0,
                                                                  political_interest_beta   = 0,
                                                                  religiosity_beta = 0,
                                                                  country="Ireland",
                                                                  local_election=0,
                                                                  mid_field_season=0,
                                                                  day_off=0), type = "response", allow.new.levels=T)

issp_2007$IE.PRED.11 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                  sleep_duration_beta = 0.827,
                                                                  sleep_duration_power2_beta = 0.889,
                                                                  urban_rural_area_beta = 0,
                                                                  age_beta  = 0, 
                                                                  degree_beta  = 0,
                                                                  income_beta   = 0,
                                                                  sex_beta   = 0,
                                                                  political_interest_beta   = 0,
                                                                  religiosity_beta = 0,
                                                                  country="Ireland",
                                                                  local_election=0,
                                                                  mid_field_season=0,
                                                                  day_off=0), type = "response", allow.new.levels=T)


issp_2007$IE.DIFF.3.7 <- issp_2007$IE.PRED.3*100 - issp_2007$IE.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 3 & country == "Ireland")$IE.DIFF.3.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 3 & country == "Ireland")$IE.DIFF.3.7)

issp_2007$IE.DIFF.4.7 <- issp_2007$IE.PRED.4*100 - issp_2007$IE.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 4 & country == "Ireland")$IE.DIFF.4.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 4 & country == "Ireland")$IE.DIFF.4.7)

issp_2007$IE.DIFF.5.7 <- issp_2007$IE.PRED.5*100 - issp_2007$IE.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 5 & country == "Ireland")$IE.DIFF.5.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 5 & country == "Ireland")$IE.DIFF.5.7)

issp_2007$IE.DIFF.6.7 <- issp_2007$IE.PRED.6*100 - issp_2007$IE.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 6 & country == "Ireland")$IE.DIFF.6.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 6 & country == "Ireland")$IE.DIFF.6.7)

issp_2007$IE.DIFF.8.7 <- issp_2007$IE.PRED.8*100 - issp_2007$IE.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 8 & country == "Ireland")$IE.DIFF.8.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 8 & country == "Ireland")$IE.DIFF.8.7)

issp_2007$IE.DIFF.9.7 <- issp_2007$IE.PRED.9*100 - issp_2007$IE.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 9 & country == "Ireland")$IE.DIFF.9.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 9 & country == "Ireland")$IE.DIFF.9.7)

issp_2007$IE.DIFF.10.7 <- issp_2007$IE.PRED.10*100 - issp_2007$IE.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 10 & country == "Ireland")$IE.DIFF.10.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 10 & country == "Ireland")$IE.DIFF.10.7)

issp_2007$IE.DIFF.11.7 <- issp_2007$IE.PRED.11*100 - issp_2007$IE.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 11 & country == "Ireland")$IE.DIFF.11.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 11 & country == "Ireland")$IE.DIFF.11.7)

mean(subset(issp_2007, subset = sleep_duration == 7 & country == "Ireland")$IE.PRED.7*100)

Country <- c("Ireland", "Ireland", "Ireland", "Ireland", "Ireland", "Ireland", "Ireland", "Ireland", "Ireland")
Sleep <- c(3, 4, 5, 6, 7, 8, 9, 10, 11)
Mean <- c(-4.448955,
          -2.27578,
          -0.7586577,
          -0.05365972, NA, 
          -0.5443537,
          -1.753551,
          -3.711611,
          -6.383856)
SE <- c(0.05609923,
        0.05527357,
        0.007781297,
        0.000535826, NA, 
        0.002030512,
        0.006983587,
        0.01733423,
        0.04694389)

IE.DIFFERENCES <- data.frame(Country, Sleep, Mean, SE)
IE.DIFFERENCES$ci.l.99 <- IE.DIFFERENCES$Mean - 2.576*IE.DIFFERENCES$SE 
IE.DIFFERENCES$ci.u.99 <- IE.DIFFERENCES$Mean + 2.576*IE.DIFFERENCES$SE 
IE.DIFFERENCES$ci.l.95 <- IE.DIFFERENCES$Mean - 1.96*IE.DIFFERENCES$SE 
IE.DIFFERENCES$ci.u.95 <- IE.DIFFERENCES$Mean + 1.96*IE.DIFFERENCES$SE

View(IE.DIFFERENCES)

PLOT.IE.DIFFERENCES <- ggplot(IE.DIFFERENCES, aes(x=Sleep, y = Mean)) +
  geom_point(position=position_dodge(width=1), size=3, colour="black")  +
  geom_errorbar(aes(ymax = ci.u.99, ymin = ci.l.99), width = 0, position=position_dodge(width=1), colour="black", size=0.5) +
  geom_errorbar(aes(ymax = ci.u.95, ymin = ci.l.95), width = 0, position=position_dodge(width=1), colour="black", size=1.5) +
  theme_classic() +
  theme(axis.text.x=element_text(colour="black", size = 12), axis.text.y=element_text(colour="black", size = 12)) +
  theme(axis.title=element_text(size=14, colour="black", face="bold"), legend.text=element_text(size=14), legend.title=element_text(size=14)) +
  labs(    x = "\nSleep Duration (in Hours)\n", 
           y = "\n Differences (%) in the Predicted Probability of \nTurnout \n from the Peak Sleep Duration (7 Hours)\n",
           title = "") +
  geom_hline(yintercept = 0, size = I(0.3), color = I("black"), linetype="dashed") +
  ggpubr::grids(linetype = "dashed") +
  scale_x_continuous(breaks=c(3, 4, 5, 6, 7, 8, 9, 10, 11)) +
  scale_y_continuous(breaks=c(-9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1), limits = c(-9, 1)) +
  theme(strip.text.x = element_text(size = 12, colour = "black")) +
  annotate("rect", xmin = 6, xmax = 10, ymin = -Inf, ymax = Inf,
           alpha = .1,fill = "blue") +
  annotate("rect", xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = -1,
           alpha = .3, fill = "grey") +
  geom_vline(xintercept = 7, size = I(0.3), color = I("black"), linetype="solid") + 
  geom_text(data = data.frame(x = 8.75, y = 1, label = "the Average Predicted Probability \nat 7 Hours of Sleep is 75%"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE, fontface = 2) +
  geom_text(data = data.frame(x = 3.55, y = -4.45386700, label = "-4.45"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 4.55, y = -2.28988100, label = "-2.28"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 5.55, y = -0.76350850, label = "-0.76"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 6.55, y = -0.07, label = "-0.05"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 8.55, y = -0.54238740, label = "-0.54"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 9.55, y = -1.75131900, label = "-1.75"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 10.55, y = -3.71700100, label = "-3.71"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 11.55, y = -6.38127600, label = "-6.38"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE)

PLOT.IE.DIFFERENCES

subset(issp_2007, subset = sleep_duration == 3 & country == "Finland", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 4 & country == "Finland", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 5 & country == "Finland", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 6 & country == "Finland", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 7 & country == "Finland", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 8 & country == "Finland", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 9 & country == "Finland", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 10 & country == "Finland", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 11 & country == "Finland", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))

issp_2007$FI.PRED.3 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -1.69,
                                                                 sleep_duration_power2_beta = -1.18,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="Finland",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$FI.PRED.4 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -1.37,
                                                                 sleep_duration_power2_beta = -1.04,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="Finland",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$FI.PRED.5 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -1.04,
                                                                 sleep_duration_power2_beta = -0.861,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="Finland",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$FI.PRED.6 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -0.706,
                                                                 sleep_duration_power2_beta = -0.644,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="Finland",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$FI.PRED.7 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -0.376,
                                                                 sleep_duration_power2_beta = -0.387,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="Finland",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$FI.PRED.8 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -0.0463,
                                                                 sleep_duration_power2_beta = -0.0903,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="Finland",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$FI.PRED.9 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = 0.283,
                                                                 sleep_duration_power2_beta = 0.246,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="Finland",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$FI.PRED.10 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                  sleep_duration_beta = 0.613,
                                                                  sleep_duration_power2_beta = 0.621,
                                                                  urban_rural_area_beta = 0,
                                                                  age_beta  = 0, 
                                                                  degree_beta  = 0,
                                                                  income_beta   = 0,
                                                                  sex_beta   = 0,
                                                                  political_interest_beta   = 0,
                                                                  religiosity_beta = 0,
                                                                  country="Finland",
                                                                  local_election=0,
                                                                  mid_field_season=0,
                                                                  day_off=0), type = "response", allow.new.levels=T)

issp_2007$FI.PRED.11 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                  sleep_duration_beta = 0.943,
                                                                  sleep_duration_power2_beta = 1.04,
                                                                  urban_rural_area_beta = 0,
                                                                  age_beta  = 0, 
                                                                  degree_beta  = 0,
                                                                  income_beta   = 0,
                                                                  sex_beta   = 0,
                                                                  political_interest_beta   = 0,
                                                                  religiosity_beta = 0,
                                                                  country="Finland",
                                                                  local_election=0,
                                                                  mid_field_season=0,
                                                                  day_off=0), type = "response", allow.new.levels=T)


issp_2007$FI.DIFF.3.6 <- issp_2007$FI.PRED.3*100 - issp_2007$FI.PRED.6*100
mean(subset(issp_2007, subset = sleep_duration == 3 & country == "Finland")$FI.DIFF.3.6)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 3 & country == "Finland")$FI.DIFF.3.6)

issp_2007$FI.DIFF.4.6 <- issp_2007$FI.PRED.4*100 - issp_2007$FI.PRED.6*100
mean(subset(issp_2007, subset = sleep_duration == 4 & country == "Finland")$FI.DIFF.4.6)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 4 & country == "Finland")$FI.DIFF.4.6)
psych::describe(subset(issp_2007, subset = sleep_duration == 4 & country == "Finland")$FI.DIFF.4.6) # 1 respondent at 4

issp_2007$FI.DIFF.5.6 <- issp_2007$FI.PRED.5*100 - issp_2007$FI.PRED.6*100
mean(subset(issp_2007, subset = sleep_duration == 5 & country == "Finland")$FI.DIFF.5.6)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 5 & country == "Finland")$FI.DIFF.5.6)

issp_2007$FI.DIFF.7.6 <- issp_2007$FI.PRED.7*100 - issp_2007$FI.PRED.6*100
mean(subset(issp_2007, subset = sleep_duration == 7 & country == "Finland")$FI.DIFF.7.6)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 7 & country == "Finland")$FI.DIFF.7.6)

issp_2007$FI.DIFF.8.6 <- issp_2007$FI.PRED.8*100 - issp_2007$FI.PRED.6*100
mean(subset(issp_2007, subset = sleep_duration == 8 & country == "Finland")$FI.DIFF.8.6)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 8 & country == "Finland")$FI.DIFF.8.6)

issp_2007$FI.DIFF.9.6 <- issp_2007$FI.PRED.9*100 - issp_2007$FI.PRED.6*100
mean(subset(issp_2007, subset = sleep_duration == 9 & country == "Finland")$FI.DIFF.9.6)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 9 & country == "Finland")$FI.DIFF.9.6)

issp_2007$FI.DIFF.10.6 <- issp_2007$FI.PRED.10*100 - issp_2007$FI.PRED.6*100
mean(subset(issp_2007, subset = sleep_duration == 10 & country == "Finland")$FI.DIFF.10.6)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 10 & country == "Finland")$FI.DIFF.10.6)

issp_2007$FI.DIFF.11.6 <- issp_2007$FI.PRED.11*100 - issp_2007$FI.PRED.6*100
mean(subset(issp_2007, subset = sleep_duration == 11 & country == "Finland")$FI.DIFF.11.6)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 11 & country == "Finland")$FI.DIFF.11.6)

mean(subset(issp_2007, subset = sleep_duration == 6 & country == "Finland")$FI.PRED.6*100)

Country <- c("Finland", "Finland", "Finland", "Finland", "Finland", "Finland", "Finland", "Finland", "Finland")
Sleep <- c(3, 4, 5, 6, 7, 8, 9, 10, 11)
Mean <- c(-0.9619588,
          -0.4471052,
          -0.1142027, NA, 
          -0.1264564,
          -0.4800128,
          -1.086766,
          -1.96658,
          -3.218722)
SE <- c(0.01819666,
        0.009068158,
        0.000889244, NA, 
        0.000429749,
        0.00088876,
        0.002886025,
        0.00819588,
        0.03854166)

FI.DIFFERENCES <- data.frame(Country, Sleep, Mean, SE)
FI.DIFFERENCES$ci.l.99 <- FI.DIFFERENCES$Mean - 2.576*FI.DIFFERENCES$SE 
FI.DIFFERENCES$ci.u.99 <- FI.DIFFERENCES$Mean + 2.576*FI.DIFFERENCES$SE 
FI.DIFFERENCES$ci.l.95 <- FI.DIFFERENCES$Mean - 1.96*FI.DIFFERENCES$SE 
FI.DIFFERENCES$ci.u.95 <- FI.DIFFERENCES$Mean + 1.96*FI.DIFFERENCES$SE 

View(FI.DIFFERENCES)

PLOT.FI.DIFFERENCES <- ggplot(FI.DIFFERENCES, aes(x=Sleep, y = Mean)) +
  geom_point(position=position_dodge(width=1), size=3, colour="black")  +
  geom_errorbar(aes(ymax = ci.u.99, ymin = ci.l.99), width = 0, position=position_dodge(width=1), colour="black", size=0.5) +
  geom_errorbar(aes(ymax = ci.u.95, ymin = ci.l.95), width = 0, position=position_dodge(width=1), colour="black", size=1.5) +
  theme_classic() +
  theme(axis.text.x=element_text(colour="black", size = 12), axis.text.y=element_text(colour="black", size = 12)) +
  theme(axis.title=element_text(size=14, colour="black", face="bold"), legend.text=element_text(size=14), legend.title=element_text(size=14)) +
  labs(    x = "\nSleep Duration (in Hours)\n", 
           y = "\n Differences (%) in the Predicted Probability of \nTurnout \n from the Peak Sleep Duration (6 Hours)\n",
           title = "") +
  geom_hline(yintercept = 0, size = I(0.3), color = I("black"), linetype="dashed") +
  ggpubr::grids(linetype = "dashed") +
  scale_x_continuous(breaks=c(3, 4, 5, 6, 7, 8, 9, 10, 11)) +
  scale_y_continuous(breaks=c(-9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1), limits = c(-9, 1)) +
  theme(strip.text.x = element_text(size = 12, colour = "black")) +
  annotate("rect", xmin = 6, xmax = 10, ymin = -Inf, ymax = Inf,
           alpha = .1,fill = "blue") +
  annotate("rect", xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = -1,
           alpha = .3, fill = "grey") +
  geom_vline(xintercept = 6, size = I(0.3), color = I("black"), linetype="solid") +
  geom_text(data = data.frame(x = 7.75, y = 1, label = "the Average Predicted Probability \nat 6 Hours of Sleep is 86%"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE, fontface = 2) +
  geom_text(data = data.frame(x = 3.55, y = -0.9640050, label = "-0.96"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 4.55, y = -0.4504627, label = "-0.45"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 5.55, y = -0.1144031, label = "-0.11"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) + 
  geom_text(data = data.frame(x = 7.55, y = -0.1329595, label = "-0.13"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 8.55, y = -0.4776385, label = "-0.48"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 9.55, y = -1.0980240, label = "-1.09"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) + 
  geom_text(data = data.frame(x = 10.55, y = -1.9720490, label = "-1.97"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 11.55, y = -3.2191140, label = "-3.22"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE)

PLOT.FI.DIFFERENCES


subset(issp_2007, subset = sleep_duration == 3 & country == "Greece", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 4 & country == "Greece", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 5 & country == "Greece", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 6 & country == "Greece", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 7 & country == "Greece", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 8 & country == "Greece", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 9 & country == "Greece", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 10 & country == "Greece", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 11 & country == "Greece", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))

# No participant with 3 hours of sleep in Greece

issp_2007$GR.PRED.4 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -1.53,
                                                                 sleep_duration_power2_beta = -1.18,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="Greece",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$GR.PRED.5 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -1.14,
                                                                 sleep_duration_power2_beta = -0.959,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="Greece",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$GR.PRED.6 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -0.746,
                                                                 sleep_duration_power2_beta = -0.689,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="Greece",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$GR.PRED.7 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -0.354,
                                                                 sleep_duration_power2_beta = -0.370,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="Greece",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$GR.PRED.8 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = 0.0380,
                                                                 sleep_duration_power2_beta = -0.00206,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="Greece",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$GR.PRED.9 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = 0.430,
                                                                 sleep_duration_power2_beta = 0.415,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="Greece",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$GR.PRED.10 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                  sleep_duration_beta = 0.822,
                                                                  sleep_duration_power2_beta = 0.881,
                                                                  urban_rural_area_beta = 0,
                                                                  age_beta  = 0, 
                                                                  degree_beta  = 0,
                                                                  income_beta   = 0,
                                                                  sex_beta   = 0,
                                                                  political_interest_beta   = 0,
                                                                  religiosity_beta = 0,
                                                                  country="Greece",
                                                                  local_election=0,
                                                                  mid_field_season=0,
                                                                  day_off=0), type = "response", allow.new.levels=T)

issp_2007$GR.PRED.11 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                  sleep_duration_beta = 1.21,
                                                                  sleep_duration_power2_beta = 1.40,
                                                                  urban_rural_area_beta = 0,
                                                                  age_beta  = 0, 
                                                                  degree_beta  = 0,
                                                                  income_beta   = 0,
                                                                  sex_beta   = 0,
                                                                  political_interest_beta   = 0,
                                                                  religiosity_beta = 0,
                                                                  country="Greece",
                                                                  local_election=0,
                                                                  mid_field_season=0,
                                                                  day_off=0), type = "response", allow.new.levels=T)


#issp_2007$GR.DIFF.3.6 <- issp_2007$GR.PRED.3*100 - issp_2007$GR.PRED.6*100
#mean(subset(issp_2007, subset = sleep_duration == 3 & country == "Greece")$GR.DIFF.3.6)
#plotrix::std.error(subset(issp_2007, subset = sleep_duration == 3 & country == "Greece")$GR.DIFF.3.6)

issp_2007$GR.DIFF.4.5 <- issp_2007$GR.PRED.4*100 - issp_2007$GR.PRED.5*100
mean(subset(issp_2007, subset = sleep_duration == 4 & country == "Greece")$GR.DIFF.4.5)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 4 & country == "Greece")$GR.DIFF.4.5)

issp_2007$GR.DIFF.6.5 <- issp_2007$GR.PRED.6*100 - issp_2007$GR.PRED.5*100
mean(subset(issp_2007, subset = sleep_duration == 6 & country == "Greece")$GR.DIFF.6.5)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 6 & country == "Greece")$GR.DIFF.6.5)

issp_2007$GR.DIFF.7.5 <- issp_2007$GR.PRED.7*100 - issp_2007$GR.PRED.5*100
mean(subset(issp_2007, subset = sleep_duration == 7 & country == "Greece")$GR.DIFF.7.5)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 7 & country == "Greece")$GR.DIFF.7.5)

issp_2007$GR.DIFF.8.5 <- issp_2007$GR.PRED.8*100 - issp_2007$GR.PRED.5*100
mean(subset(issp_2007, subset = sleep_duration == 8 & country == "Greece")$GR.DIFF.8.5)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 8 & country == "Greece")$GR.DIFF.8.5)

issp_2007$GR.DIFF.9.5 <- issp_2007$GR.PRED.9*100 - issp_2007$GR.PRED.5*100
mean(subset(issp_2007, subset = sleep_duration == 9 & country == "Greece")$GR.DIFF.9.5)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 9 & country == "Greece")$GR.DIFF.9.5)

issp_2007$GR.DIFF.10.5 <- issp_2007$GR.PRED.10*100 - issp_2007$GR.PRED.5*100
mean(subset(issp_2007, subset = sleep_duration == 10 & country == "Greece")$GR.DIFF.10.5)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 10 & country == "Greece")$GR.DIFF.10.5)

issp_2007$GR.DIFF.11.5 <- issp_2007$GR.PRED.11*100 - issp_2007$GR.PRED.5*100
mean(subset(issp_2007, subset = sleep_duration == 11 & country == "Greece")$GR.DIFF.11.5)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 11 & country == "Greece")$GR.DIFF.11.5)

mean(subset(issp_2007, subset = sleep_duration == 5 & country == "Greece")$GR.PRED.6*100)

Country <- c("Greece", "Greece", "Greece", "Greece", "Greece", "Greece", "Greece", "Greece", "Greece")
Sleep <- c(3, 4, 5, 6, 7, 8, 9, 10, 11)
Mean <- c(NA, -0.005247756, NA,
          -0.0825179,
          -0.26133,
          -0.5401858,
          -0.9233288,
          -1.421293,
          -2.064661)
SE <- c(NA, 5.03E-05, NA,
        0.000187385,
        0.000393205,
        0.000628011,
        0.001547962,
        0.004800278,
        0.005773743)

GR.DIFFERENCES <- data.frame(Country, Sleep, Mean, SE)
GR.DIFFERENCES$ci.l.99 <- GR.DIFFERENCES$Mean - 2.576*GR.DIFFERENCES$SE 
GR.DIFFERENCES$ci.u.99 <- GR.DIFFERENCES$Mean + 2.576*GR.DIFFERENCES$SE 
GR.DIFFERENCES$ci.l.95 <- GR.DIFFERENCES$Mean - 1.96*GR.DIFFERENCES$SE 
GR.DIFFERENCES$ci.u.95 <- GR.DIFFERENCES$Mean + 1.96*GR.DIFFERENCES$SE 

View(GR.DIFFERENCES)

PLOT.GR.DIFFERENCES <- ggplot(GR.DIFFERENCES, aes(x=Sleep, y = Mean)) +
  geom_point(position=position_dodge(width=1), size=3, colour="black")  +
  geom_errorbar(aes(ymax = ci.u.99, ymin = ci.l.99), width = 0, position=position_dodge(width=1), colour="black", size=0.5) +
  geom_errorbar(aes(ymax = ci.u.95, ymin = ci.l.95), width = 0, position=position_dodge(width=1), colour="black", size=1.5) +
  theme_classic() +
  theme(axis.text.x=element_text(colour="black", size = 12), axis.text.y=element_text(colour="black", size = 12)) +
  theme(axis.title=element_text(size=14, colour="black", face="bold"), legend.text=element_text(size=14), legend.title=element_text(size=14)) +
  labs(    x = "\nSleep Duration (in Hours)\n", 
           y = "\n Differences (%) in the Predicted Probability of \nTurnout \n from the Peak Sleep Duration (5 Hours)\n",
           title = "") +
  geom_hline(yintercept = 0, size = I(0.3), color = I("black"), linetype="dashed") +
  ggpubr::grids(linetype = "dashed") +
  scale_x_continuous(breaks=c(3, 4, 5, 6, 7, 8, 9, 10, 11)) +
  scale_y_continuous(breaks=c(-9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1), limits = c(-9, 1)) +
  theme(strip.text.x = element_text(size = 12, colour = "black")) +
  annotate("rect", xmin = 6, xmax = 10, ymin = -Inf, ymax = Inf,
           alpha = .1,fill = "blue") +
  annotate("rect", xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = -1,
           alpha = .3, fill = "grey") +
  geom_vline(xintercept = 5, size = I(0.3), color = I("black"), linetype="solid") +
  geom_text(data = data.frame(x = 6.625, y = 1, label = "the Average Predicted Probability \nat 5 Hours of Sleep is 91%"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE, fontface = 2) +
  geom_text(data = data.frame(x = 4.55, y = -0.1, label = "-0.01"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 6.55, y = -0.0889, label = "-0.08"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 7.55, y = -0.26370910, label = "-0.26"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 8.55, y = -0.54435410, label = "-0.54"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 9.55, y = -0.92002000, label = "-0.92"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 10.55, y = -1.42499500, label = "-1.42"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 11.55, y = -2.06671400, label = "-2.06"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE)


PLOT.GR.DIFFERENCES

subset(issp_2007, subset = sleep_duration == 3 & country == "Mexico", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 4 & country == "Mexico", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 5 & country == "Mexico", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 6 & country == "Mexico", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 7 & country == "Mexico", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 8 & country == "Mexico", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 9 & country == "Mexico", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 10 & country == "Mexico", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 11 & country == "Mexico", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))



issp_2007$MX.PRED.3 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -1.41,
                                                                 sleep_duration_power2_beta =  -0.985,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="Mexico",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$MX.PRED.4 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -1.13,
                                                                 sleep_duration_power2_beta = -0.867,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="Mexico",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$MX.PRED.5 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -0.848,
                                                                 sleep_duration_power2_beta = -0.715,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="Mexico",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$MX.PRED.6 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -0.567,
                                                                 sleep_duration_power2_beta = -0.529,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="Mexico",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$MX.PRED.7 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -0.285,
                                                                 sleep_duration_power2_beta = -0.310,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="Mexico",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$MX.PRED.8 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -0.00393,
                                                                 sleep_duration_power2_beta = -0.0570,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="Mexico",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$MX.PRED.9 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = 0.277,
                                                                 sleep_duration_power2_beta = 0.230,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="Mexico",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$MX.PRED.10 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                  sleep_duration_beta = 0.559,
                                                                  sleep_duration_power2_beta = 0.550,
                                                                  urban_rural_area_beta = 0,
                                                                  age_beta  = 0, 
                                                                  degree_beta  = 0,
                                                                  income_beta   = 0,
                                                                  sex_beta   = 0,
                                                                  political_interest_beta   = 0,
                                                                  religiosity_beta = 0,
                                                                  country="Mexico",
                                                                  local_election=0,
                                                                  mid_field_season=0,
                                                                  day_off=0), type = "response", allow.new.levels=T)

issp_2007$MX.PRED.11 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                  sleep_duration_beta = 0.840,
                                                                  sleep_duration_power2_beta = 0.904,
                                                                  urban_rural_area_beta = 0,
                                                                  age_beta  = 0, 
                                                                  degree_beta  = 0,
                                                                  income_beta   = 0,
                                                                  sex_beta   = 0,
                                                                  political_interest_beta   = 0,
                                                                  religiosity_beta = 0,
                                                                  country="Mexico",
                                                                  local_election=0,
                                                                  mid_field_season=0,
                                                                  day_off=0), type = "response", allow.new.levels=T)


issp_2007$MX.DIFF.3.7 <- issp_2007$MX.PRED.3*100 - issp_2007$MX.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 3 & country == "Mexico")$MX.DIFF.3.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 3 & country == "Mexico")$MX.DIFF.3.7)

issp_2007$MX.DIFF.4.7 <- issp_2007$MX.PRED.4*100 - issp_2007$MX.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 4 & country == "Mexico")$MX.DIFF.4.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 4 & country == "Mexico")$MX.DIFF.4.7)

issp_2007$MX.DIFF.5.7 <- issp_2007$MX.PRED.5*100 - issp_2007$MX.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 5 & country == "Mexico")$MX.DIFF.5.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 5 & country == "Mexico")$MX.DIFF.5.7)

issp_2007$MX.DIFF.6.7 <- issp_2007$MX.PRED.6*100 - issp_2007$MX.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 6 & country == "Mexico")$MX.DIFF.6.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 6 & country == "Mexico")$MX.DIFF.6.7)

issp_2007$MX.DIFF.8.7 <- issp_2007$MX.PRED.8*100 - issp_2007$MX.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 8 & country == "Mexico")$MX.DIFF.8.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 8 & country == "Mexico")$MX.DIFF.8.7)

issp_2007$MX.DIFF.9.7 <- issp_2007$MX.PRED.9*100 - issp_2007$MX.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 9 & country == "Mexico")$MX.DIFF.9.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 9 & country == "Mexico")$MX.DIFF.9.7)

issp_2007$MX.DIFF.10.7 <- issp_2007$MX.PRED.10*100 - issp_2007$MX.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 10 & country == "Mexico")$MX.DIFF.10.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 10 & country == "Mexico")$MX.DIFF.10.7)

issp_2007$MX.DIFF.11.7 <- issp_2007$MX.PRED.11*100 - issp_2007$MX.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 11 & country == "Mexico")$MX.DIFF.11.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 11 & country == "Mexico")$MX.DIFF.11.7)

mean(subset(issp_2007, subset = sleep_duration == 7 & country == "Mexico")$MX.PRED.7*100)

Country <- c("Mexico", "Mexico", "Mexico", "Mexico", "Mexico", "Mexico", "Mexico", "Mexico", "Mexico")
Sleep <- c(3, 4, 5, 6, 7, 8, 9, 10, 11)
Mean <- c(-1.339902,
          -0.5480297,
          -0.0717156,
          0.0959625, NA, 
          -0.3804578,
          -1.050251,
          -2.050174,
          -3.451061)
SE <- c(0.036746,
        0.007814213,
        0.000582594,
        0.000413549, NA, 
        0.000873004,
        0.003158524,
        0.00804422,
        0.02015755)

MX.DIFFERENCES <- data.frame(Country, Sleep, Mean, SE)
MX.DIFFERENCES$ci.l.99 <- MX.DIFFERENCES$Mean - 2.576*MX.DIFFERENCES$SE 
MX.DIFFERENCES$ci.u.99 <- MX.DIFFERENCES$Mean + 2.576*MX.DIFFERENCES$SE 
MX.DIFFERENCES$ci.l.95 <- MX.DIFFERENCES$Mean - 1.96*MX.DIFFERENCES$SE 
MX.DIFFERENCES$ci.u.95 <- MX.DIFFERENCES$Mean + 1.96*MX.DIFFERENCES$SE 

View(MX.DIFFERENCES)

PLOT.MX.DIFFERENCES <- ggplot(MX.DIFFERENCES, aes(x=Sleep, y = Mean)) +
  geom_point(position=position_dodge(width=1), size=3, colour="black")  +
  geom_errorbar(aes(ymax = ci.u.99, ymin = ci.l.99), width = 0, position=position_dodge(width=1), colour="black", size=0.5) +
  geom_errorbar(aes(ymax = ci.u.95, ymin = ci.l.95), width = 0, position=position_dodge(width=1), colour="black", size=1.5) +
  theme_classic() +
  theme(axis.text.x=element_text(colour="black", size = 12), axis.text.y=element_text(colour="black", size = 12)) +
  theme(axis.title=element_text(size=14, colour="black", face="bold"), legend.text=element_text(size=14), legend.title=element_text(size=14)) +
  labs(    x = "\nSleep Duration (in Hours)\n", 
           y = "\n Differences (%) in the Predicted Probability of \nTurnout \n from the Peak Sleep Duration (7 Hours)\n",
           title = "") +
  geom_hline(yintercept = 0, size = I(0.3), color = I("black"), linetype="dashed") +
  ggpubr::grids(linetype = "dashed") +
  scale_x_continuous(breaks=c(3, 4, 5, 6, 7, 8, 9, 10, 11)) +
  scale_y_continuous(breaks=c(-9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1), limits = c(-9, 1)) +
  theme(strip.text.x = element_text(size = 12, colour = "black")) +
  annotate("rect", xmin = 6, xmax = 10, ymin = -Inf, ymax = Inf,
           alpha = .1,fill = "blue") +
  annotate("rect", xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = -1,
           alpha = .3, fill = "grey") +
  geom_vline(xintercept = 7, size = I(0.3), color = I("black"), linetype="solid") + 
  geom_text(data = data.frame(x = 8.75, y = 1, label = "the Average Predicted Probability \nat 7 Hours of Sleep is 83%"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE, fontface = 2) +
  geom_text(data = data.frame(x = 3.55, y = -1.34473000, label = "-1.34"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) + 
  geom_text(data = data.frame(x = 4.55, y = -0.55478680, label = "-0.55"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 5.55, y = -0.07265145, label = "-0.07"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 6.55, y = 0.10984764, label = "0.10"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 8.55, y = -0.38329480, label = "-0.38"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 9.55, y = -1.05567600, label = "-1.05"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 10.55, y = -2.05858500, label = "-2.05"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 11.55, y = -3.45866600, label = "-3.45"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE)


PLOT.MX.DIFFERENCES

subset(issp_2007, subset = sleep_duration == 3 & country == "the Netherlands", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 4 & country == "the Netherlands", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 5 & country == "the Netherlands", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 6 & country == "the Netherlands", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 7 & country == "the Netherlands", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 8 & country == "the Netherlands", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 9 & country == "the Netherlands", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 10 & country == "the Netherlands", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 11 & country == "the Netherlands", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))



issp_2007$NL.PRED.3 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -1.66,
                                                                 sleep_duration_power2_beta =  -1.13,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="the Netherlands",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$NL.PRED.4 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -1.35 ,
                                                                 sleep_duration_power2_beta = -1.01,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="the Netherlands",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$NL.PRED.5 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -1.04,
                                                                 sleep_duration_power2_beta = -0.844,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="the Netherlands",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$NL.PRED.6 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -0.725,
                                                                 sleep_duration_power2_beta = -0.646,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="the Netherlands",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$NL.PRED.7 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -0.414,
                                                                 sleep_duration_power2_beta = -0.412,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="the Netherlands",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$NL.PRED.8 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -0.102,
                                                                 sleep_duration_power2_beta = -0.143,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="the Netherlands",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$NL.PRED.9 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = 0.209,
                                                                 sleep_duration_power2_beta = 0.163,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="the Netherlands",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$NL.PRED.10 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                  sleep_duration_beta = 0.520,
                                                                  sleep_duration_power2_beta = 0.504,
                                                                  urban_rural_area_beta = 0,
                                                                  age_beta  = 0, 
                                                                  degree_beta  = 0,
                                                                  income_beta   = 0,
                                                                  sex_beta   = 0,
                                                                  political_interest_beta   = 0,
                                                                  religiosity_beta = 0,
                                                                  country="the Netherlands",
                                                                  local_election=0,
                                                                  mid_field_season=0,
                                                                  day_off=0), type = "response", allow.new.levels=T)

issp_2007$NL.PRED.11 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                  sleep_duration_beta = 0.832,
                                                                  sleep_duration_power2_beta = 0.882,
                                                                  urban_rural_area_beta = 0,
                                                                  age_beta  = 0, 
                                                                  degree_beta  = 0,
                                                                  income_beta   = 0,
                                                                  sex_beta   = 0,
                                                                  political_interest_beta   = 0,
                                                                  religiosity_beta = 0,
                                                                  country="the Netherlands",
                                                                  local_election=0,
                                                                  mid_field_season=0,
                                                                  day_off=0), type = "response", allow.new.levels=T)


issp_2007$NL.DIFF.3.6 <- issp_2007$NL.PRED.3*100 - issp_2007$NL.PRED.6*100
mean(subset(issp_2007, subset = sleep_duration == 3 & country == "the Netherlands")$NL.DIFF.3.6)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 3 & country == "the Netherlands")$NL.DIFF.3.6)
psych::describe(subset(issp_2007, subset = sleep_duration == 3 & country == "the Netherlands")$NL.DIFF.3.6) # 1 respondent at 3

issp_2007$NL.DIFF.4.6 <- issp_2007$NL.PRED.4*100 - issp_2007$NL.PRED.6*100
mean(subset(issp_2007, subset = sleep_duration == 4 & country == "the Netherlands")$NL.DIFF.4.6)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 4 & country == "the Netherlands")$NL.DIFF.4.6)

issp_2007$NL.DIFF.5.6 <- issp_2007$NL.PRED.5*100 - issp_2007$NL.PRED.6*100
mean(subset(issp_2007, subset = sleep_duration == 5 & country == "the Netherlands")$NL.DIFF.5.6)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 5 & country == "the Netherlands")$NL.DIFF.5.6)

issp_2007$NL.DIFF.7.6 <- issp_2007$NL.PRED.7*100 - issp_2007$NL.PRED.6*100
mean(subset(issp_2007, subset = sleep_duration == 7 & country == "the Netherlands")$NL.DIFF.7.6)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 7 & country == "the Netherlands")$NL.DIFF.7.6)

issp_2007$NL.DIFF.8.6 <- issp_2007$NL.PRED.8*100 - issp_2007$NL.PRED.6*100
mean(subset(issp_2007, subset = sleep_duration == 8 & country == "the Netherlands")$NL.DIFF.8.6)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 8 & country == "the Netherlands")$NL.DIFF.8.6)

issp_2007$NL.DIFF.9.6 <- issp_2007$NL.PRED.9*100 - issp_2007$NL.PRED.6*100
mean(subset(issp_2007, subset = sleep_duration == 9 & country == "the Netherlands")$NL.DIFF.9.6)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 9 & country == "the Netherlands")$NL.DIFF.9.6)

issp_2007$NL.DIFF.10.6 <- issp_2007$NL.PRED.10*100 - issp_2007$NL.PRED.6*100
mean(subset(issp_2007, subset = sleep_duration == 10 & country == "the Netherlands")$NL.DIFF.10.6)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 10 & country == "the Netherlands")$NL.DIFF.10.6)

issp_2007$NL.DIFF.11.6 <- issp_2007$NL.PRED.11*100 - issp_2007$NL.PRED.6*100
mean(subset(issp_2007, subset = sleep_duration == 11 & country == "the Netherlands")$NL.DIFF.11.6)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 11 & country == "the Netherlands")$NL.DIFF.11.6)

mean(subset(issp_2007, subset = sleep_duration == 6 & country == "the Netherlands")$NL.PRED.7*100)

Country <- c("the Netherlands", "the Netherlands", "the Netherlands", "the Netherlands", "the Netherlands", "the Netherlands", "the Netherlands", "the Netherlands", "the Netherlands")
Sleep <- c(3, 4, 5, 6, 7, 8, 9, 10, 11)
Mean <- c(-0.4609983,
          -0.1665685,
          -0.0341577,
          NA,
          -0.09600899,
          -0.310148,
          -0.6607226,
          -1.148474,
          -1.795427)
SE <- c(0,
        0.00105369,
        0.000120115,
        NA,
        7.83E-05,
        0.000176187,
        0.000339863,
        0.001244368,
        0.003685881)

NL.DIFFERENCES <- data.frame(Country, Sleep, Mean, SE)
NL.DIFFERENCES$ci.l.99 <- NL.DIFFERENCES$Mean - 2.576*NL.DIFFERENCES$SE 
NL.DIFFERENCES$ci.u.99 <- NL.DIFFERENCES$Mean + 2.576*NL.DIFFERENCES$SE 
NL.DIFFERENCES$ci.l.95 <- NL.DIFFERENCES$Mean - 1.96*NL.DIFFERENCES$SE 
NL.DIFFERENCES$ci.u.95 <- NL.DIFFERENCES$Mean + 1.96*NL.DIFFERENCES$SE 

View(NL.DIFFERENCES)

PLOT.NL.DIFFERENCES <- ggplot(NL.DIFFERENCES, aes(x=Sleep, y = Mean)) +
  geom_point(position=position_dodge(width=1), size=3, colour="black")  +
  geom_errorbar(aes(ymax = ci.u.99, ymin = ci.l.99), width = 0, position=position_dodge(width=1), colour="black", size=0.5) +
  geom_errorbar(aes(ymax = ci.u.95, ymin = ci.l.95), width = 0, position=position_dodge(width=1), colour="black", size=1.5) +
  theme_classic() +
  theme(axis.text.x=element_text(colour="black", size = 12), axis.text.y=element_text(colour="black", size = 12)) +
  theme(axis.title=element_text(size=14, colour="black", face="bold"), legend.text=element_text(size=14), legend.title=element_text(size=14)) +
  labs(    x = "\nSleep Duration (in Hours)\n", 
           y = "\n Differences (%) in the Predicted Probability of \nTurnout \n from the Peak Sleep Duration (6 Hours)\n",
           title = "") +
  geom_hline(yintercept = 0, size = I(0.3), color = I("black"), linetype="dashed") +
  ggpubr::grids(linetype = "dashed") +
  scale_x_continuous(breaks=c(3, 4, 5, 6, 7, 8, 9, 10, 11)) +
  scale_y_continuous(breaks=c(-9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1), limits = c(-9, 1)) +
  theme(strip.text.x = element_text(size = 12, colour = "black")) +
  annotate("rect", xmin = 6, xmax = 10, ymin = -Inf, ymax = Inf,
           alpha = .1,fill = "blue") +
  annotate("rect", xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = -1,
           alpha = .3, fill = "grey") +
  geom_vline(xintercept = 6, size = I(0.3), color = I("black"), linetype="solid") + 
  geom_text(data = data.frame(x = 7.75, y = 1, label = "the Average Predicted Probability \nat 6 Hours of Sleep is 89%"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE, fontface = 2) +
  geom_text(data = data.frame(x = 3.55, y = -0.46713000, label = "-0.46"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) + 
  geom_text(data = data.frame(x = 4.55, y = -0.17734190, label = "-0.17"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) + 
  geom_text(data = data.frame(x = 5.55, y = -0.09, label = "-0.03"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) + 
  geom_text(data = data.frame(x = 7.55, y = -0.10, label = "-0.10"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) + 
  geom_text(data = data.frame(x = 8.55, y = -0.31478130, label = "-0.31"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) + 
  geom_text(data = data.frame(x = 9.55, y = -0.66296350, label = "-0.66"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) + 
  geom_text(data = data.frame(x = 10.55, y = -1.15273230, label = "-1.15"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) + 
  geom_text(data = data.frame(x = 11.55, y = -1.80942100, label = "-1.80"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE)

PLOT.NL.DIFFERENCES

#subset(issp_2007, subset = sleep_duration == 3 & country == "New Zealand", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 4 & country == "New Zealand", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 5 & country == "New Zealand", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 6 & country == "New Zealand", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 7 & country == "New Zealand", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 8 & country == "New Zealand", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 9 & country == "New Zealand", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 10 & country == "New Zealand", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 11 & country == "New Zealand", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))

# No participant with 3 hours of sleep in New Zealand


issp_2007$NZ.PRED.4 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -1.51,
                                                                 sleep_duration_power2_beta = -1.13,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="New Zealand",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$NZ.PRED.5 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -1.18,
                                                                 sleep_duration_power2_beta = -0.963,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="New Zealand",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$NZ.PRED.6 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -0.848,
                                                                 sleep_duration_power2_beta = -0.754,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="New Zealand",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$NZ.PRED.7 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -0.518,
                                                                 sleep_duration_power2_beta = -0.508,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="New Zealand",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$NZ.PRED.8 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -0.188,
                                                                 sleep_duration_power2_beta = -0.223,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="New Zealand",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$NZ.PRED.9 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = 0.142,
                                                                 sleep_duration_power2_beta = 0.0995,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="New Zealand",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$NZ.PRED.10 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                  sleep_duration_beta = 0.471,
                                                                  sleep_duration_power2_beta = 0.460,
                                                                  urban_rural_area_beta = 0,
                                                                  age_beta  = 0, 
                                                                  degree_beta  = 0,
                                                                  income_beta   = 0,
                                                                  sex_beta   = 0,
                                                                  political_interest_beta   = 0,
                                                                  religiosity_beta = 0,
                                                                  country="New Zealand",
                                                                  local_election=0,
                                                                  mid_field_season=0,
                                                                  day_off=0), type = "response", allow.new.levels=T)

issp_2007$NZ.PRED.11 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                  sleep_duration_beta = 0.801,
                                                                  sleep_duration_power2_beta = 0.858,
                                                                  urban_rural_area_beta = 0,
                                                                  age_beta  = 0, 
                                                                  degree_beta  = 0,
                                                                  income_beta   = 0,
                                                                  sex_beta   = 0,
                                                                  political_interest_beta   = 0,
                                                                  religiosity_beta = 0,
                                                                  country="New Zealand",
                                                                  local_election=0,
                                                                  mid_field_season=0,
                                                                  day_off=0), type = "response", allow.new.levels=T)


issp_2007$NZ.DIFF.4.7 <- issp_2007$NZ.PRED.4*100 - issp_2007$NZ.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 4 & country == "New Zealand")$NZ.DIFF.4.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 4 & country == "New Zealand")$NZ.DIFF.4.7)

issp_2007$NZ.DIFF.5.7 <- issp_2007$NZ.PRED.5*100 - issp_2007$NZ.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 5 & country == "New Zealand")$NZ.DIFF.5.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 5 & country == "New Zealand")$NZ.DIFF.5.7)

issp_2007$NZ.DIFF.6.7 <- issp_2007$NZ.PRED.6*100 - issp_2007$NZ.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 6 & country == "New Zealand")$NZ.DIFF.6.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 6 & country == "New Zealand")$NZ.DIFF.6.7)

issp_2007$NZ.DIFF.8.7 <- issp_2007$NZ.PRED.8*100 - issp_2007$NZ.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 8 & country == "New Zealand")$NZ.DIFF.8.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 8 & country == "New Zealand")$NZ.DIFF.8.7)

issp_2007$NZ.DIFF.9.7 <- issp_2007$NZ.PRED.9*100 - issp_2007$NZ.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 9 & country == "New Zealand")$NZ.DIFF.9.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 9 & country == "New Zealand")$NZ.DIFF.9.7)

issp_2007$NZ.DIFF.10.7 <- issp_2007$NZ.PRED.10*100 - issp_2007$NZ.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 10 & country == "New Zealand")$NZ.DIFF.10.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 10 & country == "New Zealand")$NZ.DIFF.10.7)

issp_2007$NZ.DIFF.11.7 <- issp_2007$NZ.PRED.11*100 - issp_2007$NZ.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 11 & country == "New Zealand")$NZ.DIFF.11.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 11 & country == "New Zealand")$NZ.DIFF.11.7)

mean(subset(issp_2007, subset = sleep_duration == 7 & country == "New Zealand")$NZ.PRED.6*100)

Country <- c("New Zealand", "New Zealand", "New Zealand", "New Zealand", "New Zealand", "New Zealand", "New Zealand", "New Zealand", "New Zealand")
Sleep <- c(3, 4, 5, 6, 7, 8, 9, 10, 11)
Mean <- c(NA, 0.1830109,
          0.1789487,
          0.1107314,
          NA,
          -0.1613899,
          -0.3723792,
          -0.6393526,
          -0.9614772)
SE <- c(NA, 0.00412542,
        0.004852501,
        0.000767486,
        NA,
        0.0003746,
        0.000897601,
        0.001791758,
        0.004944052)

NZ.DIFFERENCES <- data.frame(Country, Sleep, Mean, SE)
NZ.DIFFERENCES$ci.l.99 <- NZ.DIFFERENCES$Mean - 2.576*NZ.DIFFERENCES$SE 
NZ.DIFFERENCES$ci.u.99 <- NZ.DIFFERENCES$Mean + 2.576*NZ.DIFFERENCES$SE 
NZ.DIFFERENCES$ci.l.95 <- NZ.DIFFERENCES$Mean - 1.96*NZ.DIFFERENCES$SE 
NZ.DIFFERENCES$ci.u.95 <- NZ.DIFFERENCES$Mean + 1.96*NZ.DIFFERENCES$SE 

View(NZ.DIFFERENCES)

PLOT.NZ.DIFFERENCES <- ggplot(NZ.DIFFERENCES, aes(x=Sleep, y = Mean)) +
  geom_point(position=position_dodge(width=1), size=3, colour="black")  +
  geom_errorbar(aes(ymax = ci.u.99, ymin = ci.l.99), width = 0, position=position_dodge(width=1), colour="black", size=0.5) +
  geom_errorbar(aes(ymax = ci.u.95, ymin = ci.l.95), width = 0, position=position_dodge(width=1), colour="black", size=1.5) +
  theme_classic() +
  theme(axis.text.x=element_text(colour="black", size = 12), axis.text.y=element_text(colour="black", size = 12)) +
  theme(axis.title=element_text(size=14, colour="black", face="bold"), legend.text=element_text(size=14), legend.title=element_text(size=14)) +
  labs(    x = "\nSleep Duration (in Hours)\n", 
           y = "\n Differences (%) in the Predicted Probability of \nTurnout \n from the Select Sleep Duration (7 Hours)\n",
           title = "") +
  geom_hline(yintercept = 0, size = I(0.3), color = I("black"), linetype="dashed") +
  ggpubr::grids(linetype = "dashed") +
  scale_x_continuous(breaks=c(3, 4, 5, 6, 7, 8, 9, 10, 11)) +
  scale_y_continuous(breaks=c(-9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1), limits = c(-9, 1)) +
  theme(strip.text.x = element_text(size = 12, colour = "black")) +
  annotate("rect", xmin = 6, xmax = 10, ymin = -Inf, ymax = Inf,
           alpha = .1,fill = "blue") +
  annotate("rect", xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = -1,
           alpha = .3, fill = "grey") +
  geom_vline(xintercept = 7, size = I(0.3), color = I("black"), linetype="solid") +
  geom_text(data = data.frame(x = 8.625, y = 1, label = "the Average Predicted Probability \nat 7 Hours of Sleep is 92%"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE, fontface = 2) +
  geom_text(data = data.frame(x = 4.55, y = 0.18019380, label = "0.18"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 5.55, y = 0.1801938, label = "0.18"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 6.55, y = 0.1101938, label = "0.11"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 8.55, y = -0.16797380, label = "-0.16"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 9.55, y = -0.37472240, label = "-0.37"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 10.55, y = -0.64787620, label = "-0.64"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 11.55, y = -0.96001100, label = "-0.96"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE)

PLOT.NZ.DIFFERENCES

subset(issp_2007, subset = sleep_duration == 3 & country == "the Philippines", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 4 & country == "the Philippines", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 5 & country == "the Philippines", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 6 & country == "the Philippines", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 7 & country == "the Philippines", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 8 & country == "the Philippines", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 9 & country == "the Philippines", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 10 & country == "the Philippines", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 11 & country == "the Philippines", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))



issp_2007$PH.PRED.3 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -1.40,
                                                                 sleep_duration_power2_beta =  -0.995,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="the Philippines",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$PH.PRED.4 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -1.13,
                                                                 sleep_duration_power2_beta = -0.883,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="the Philippines",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$PH.PRED.5 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -0.865,
                                                                 sleep_duration_power2_beta = -0.740,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="the Philippines",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$PH.PRED.6 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -0.598,
                                                                 sleep_duration_power2_beta = -0.565,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="the Philippines",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$PH.PRED.7 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -0.332 ,
                                                                 sleep_duration_power2_beta = -0.358,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="the Philippines",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$PH.PRED.8 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -0.0651,
                                                                 sleep_duration_power2_beta = -0.119,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="the Philippines",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$PH.PRED.9 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = 0.201,
                                                                 sleep_duration_power2_beta = 0.151,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="the Philippines",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$PH.PRED.10 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                  sleep_duration_beta = 0.468,
                                                                  sleep_duration_power2_beta = 0.454,
                                                                  urban_rural_area_beta = 0,
                                                                  age_beta  = 0, 
                                                                  degree_beta  = 0,
                                                                  income_beta   = 0,
                                                                  sex_beta   = 0,
                                                                  political_interest_beta   = 0,
                                                                  religiosity_beta = 0,
                                                                  country="the Philippines",
                                                                  local_election=0,
                                                                  mid_field_season=0,
                                                                  day_off=0), type = "response", allow.new.levels=T)

issp_2007$PH.PRED.11 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                  sleep_duration_beta = 0.734,
                                                                  sleep_duration_power2_beta = 0.788,
                                                                  urban_rural_area_beta = 0,
                                                                  age_beta  = 0, 
                                                                  degree_beta  = 0,
                                                                  income_beta   = 0,
                                                                  sex_beta   = 0,
                                                                  political_interest_beta   = 0,
                                                                  religiosity_beta = 0,
                                                                  country="the Philippines",
                                                                  local_election=0,
                                                                  mid_field_season=0,
                                                                  day_off=0), type = "response", allow.new.levels=T)


issp_2007$PH.DIFF.3.7 <- issp_2007$PH.PRED.3*100 - issp_2007$PH.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 3 & country == "the Philippines")$PH.DIFF.3.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 3 & country == "the Philippines")$PH.DIFF.3.7)

issp_2007$PH.DIFF.4.7 <- issp_2007$PH.PRED.4*100 - issp_2007$PH.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 4 & country == "the Philippines")$PH.DIFF.4.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 4 & country == "the Philippines")$PH.DIFF.4.7)

issp_2007$PH.DIFF.5.7 <- issp_2007$PH.PRED.5*100 - issp_2007$PH.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 5 & country == "the Philippines")$PH.DIFF.5.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 5 & country == "the Philippines")$PH.DIFF.5.7)

issp_2007$PH.DIFF.6.7 <- issp_2007$PH.PRED.6*100 - issp_2007$PH.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 6 & country == "the Philippines")$PH.DIFF.6.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 6 & country == "the Philippines")$PH.DIFF.6.7)

issp_2007$PH.DIFF.8.7 <- issp_2007$PH.PRED.8*100 - issp_2007$PH.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 8 & country == "the Philippines")$PH.DIFF.8.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 8 & country == "the Philippines")$PH.DIFF.8.7)

issp_2007$PH.DIFF.9.7 <- issp_2007$PH.PRED.9*100 - issp_2007$PH.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 9 & country == "the Philippines")$PH.DIFF.9.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 9 & country == "the Philippines")$PH.DIFF.9.7)

issp_2007$PH.DIFF.10.7 <- issp_2007$PH.PRED.10*100 - issp_2007$PH.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 10 & country == "the Philippines")$PH.DIFF.10.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 10 & country == "the Philippines")$PH.DIFF.10.7)

issp_2007$PH.DIFF.11.7 <- issp_2007$PH.PRED.11*100 - issp_2007$PH.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 11 & country == "the Philippines")$PH.DIFF.11.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 11 & country == "the Philippines")$PH.DIFF.11.7)

mean(subset(issp_2007, subset = sleep_duration == 7 & country == "the Philippines")$PH.PRED.7*100)

Country <- c("the Philippines", "the Philippines", "the Philippines", "the Philippines", "the Philippines", "the Philippines", "the Philippines", "the Philippines", "the Philippines")
Sleep <- c(3, 4, 5, 6, 7, 8, 9, 10, 11)
Mean <- c(-1.06576,
          -0.3972996,
          -0.03409614,
          0.09795761, NA, 
          -0.3190568,
          -0.8704178,
          -1.680328,
          -2.780772)
SE <- c(0.01231754,
        0.003740269,
        0.000217402,
        0.000297846, NA, 
        0.000789208,
        0.002472713,
        0.004973025,
        0.01263643)

PH.DIFFERENCES <- data.frame(Country, Sleep, Mean, SE)
PH.DIFFERENCES$ci.l.99 <- PH.DIFFERENCES$Mean - 2.576*PH.DIFFERENCES$SE 
PH.DIFFERENCES$ci.u.99 <- PH.DIFFERENCES$Mean + 2.576*PH.DIFFERENCES$SE 
PH.DIFFERENCES$ci.l.95 <- PH.DIFFERENCES$Mean - 1.96*PH.DIFFERENCES$SE 
PH.DIFFERENCES$ci.u.95 <- PH.DIFFERENCES$Mean + 1.96*PH.DIFFERENCES$SE 

View(PH.DIFFERENCES)

PLOT.PH.DIFFERENCES <- ggplot(PH.DIFFERENCES, aes(x=Sleep, y = Mean)) +
  geom_point(position=position_dodge(width=1), size=3, colour="black")  +
  geom_errorbar(aes(ymax = ci.u.99, ymin = ci.l.99), width = 0, position=position_dodge(width=1), colour="black", size=0.5) +
  geom_errorbar(aes(ymax = ci.u.95, ymin = ci.l.95), width = 0, position=position_dodge(width=1), colour="black", size=1.5) +
  theme_classic() +
  theme(axis.text.x=element_text(colour="black", size = 12), axis.text.y=element_text(colour="black", size = 12)) +
  theme(axis.title=element_text(size=14, colour="black", face="bold"), legend.text=element_text(size=14), legend.title=element_text(size=14)) +
  labs(    x = "\nSleep Duration (in Hours)\n", 
           y = "\n Differences (%) in the Predicted Probability of \nTurnout \n from the Peak Sleep Duration (7 Hours)\n",
           title = "") +
  geom_hline(yintercept = 0, size = I(0.3), color = I("black"), linetype="dashed") +
  ggpubr::grids(linetype = "dashed") +
  scale_x_continuous(breaks=c(3, 4, 5, 6, 7, 8, 9, 10, 11)) +
  scale_y_continuous(breaks=c(-9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1), limits = c(-9, 1)) +
  theme(strip.text.x = element_text(size = 12, colour = "black")) +
  annotate("rect", xmin = 6, xmax = 10, ymin = -Inf, ymax = Inf,
           alpha = .1,fill = "blue") +
  annotate("rect", xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = -1,
           alpha = .3, fill = "grey") +
  geom_vline(xintercept = 7, size = I(0.3), color = I("black"), linetype="solid") + 
  geom_text(data = data.frame(x = 8.75, y = 1, label = "the Average Predicted Probability \nat 7 Hours of Sleep is 85%"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE, fontface = 2) +
  geom_text(data = data.frame(x = 3.55, y = -1.07609100, label = "-1.07"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 4.55, y = -0.40034930, label = "-0.40"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 5.55, y = -0.09, label = "-0.03"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 6.55, y = 0.10, label = "0.10"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 8.55, y = -0.31740830, label = "-0.32"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 9.55, y = -0.87192480, label = "-0.87"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 10.55, y = -1.68219700, label = "-1.68"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 11.55, y = -2.78310300, label = "-2.78"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE)

PLOT.PH.DIFFERENCES

subset(issp_2007, subset = sleep_duration == 3 & country == "Russia", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 4 & country == "Russia", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 5 & country == "Russia", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 6 & country == "Russia", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 7 & country == "Russia", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 8 & country == "Russia", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 9 & country == "Russia", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 10 & country == "Russia", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 11 & country == "Russia", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))



issp_2007$RU.PRED.3 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -1.36,
                                                                 sleep_duration_power2_beta =  -0.986,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="Russia",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$RU.PRED.4 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -1.08,
                                                                 sleep_duration_power2_beta = -0.865,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="Russia",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$RU.PRED.5 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -0.807,
                                                                 sleep_duration_power2_beta = -0.709,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="Russia",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$RU.PRED.6 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta =-0.530,
                                                                 sleep_duration_power2_beta = -0.518,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="Russia",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$RU.PRED.7 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta =  -0.253,
                                                                 sleep_duration_power2_beta = -0.293,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="Russia",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$RU.PRED.8 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = 0.0240,
                                                                 sleep_duration_power2_beta = -0.0326,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="Russia",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$RU.PRED.9 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = 0.301,
                                                                 sleep_duration_power2_beta = 0.262,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="Russia",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$RU.PRED.10 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                  sleep_duration_beta = 0.578,
                                                                  sleep_duration_power2_beta = 0.592,
                                                                  urban_rural_area_beta = 0,
                                                                  age_beta  = 0, 
                                                                  degree_beta  = 0,
                                                                  income_beta   = 0,
                                                                  sex_beta   = 0,
                                                                  political_interest_beta   = 0,
                                                                  religiosity_beta = 0,
                                                                  country="Russia",
                                                                  local_election=0,
                                                                  mid_field_season=0,
                                                                  day_off=0), type = "response", allow.new.levels=T)

issp_2007$RU.PRED.11 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                  sleep_duration_beta = 0.855,
                                                                  sleep_duration_power2_beta = 0.956,
                                                                  urban_rural_area_beta = 0,
                                                                  age_beta  = 0, 
                                                                  degree_beta  = 0,
                                                                  income_beta   = 0,
                                                                  sex_beta   = 0,
                                                                  political_interest_beta   = 0,
                                                                  religiosity_beta = 0,
                                                                  country="Russia",
                                                                  local_election=0,
                                                                  mid_field_season=0,
                                                                  day_off=0), type = "response", allow.new.levels=T)


issp_2007$RU.DIFF.3.7 <- issp_2007$RU.PRED.3*100 - issp_2007$RU.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 3 & country == "Russia")$RU.DIFF.3.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 3 & country == "Russia")$RU.DIFF.3.7)

issp_2007$RU.DIFF.4.7 <- issp_2007$RU.PRED.4*100 - issp_2007$RU.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 4 & country == "Russia")$RU.DIFF.4.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 4 & country == "Russia")$RU.DIFF.4.7)

issp_2007$RU.DIFF.5.7 <- issp_2007$RU.PRED.5*100 - issp_2007$RU.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 5 & country == "Russia")$RU.DIFF.5.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 5 & country == "Russia")$RU.DIFF.5.7)

issp_2007$RU.DIFF.6.7 <- issp_2007$RU.PRED.6*100 - issp_2007$RU.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 6 & country == "Russia")$RU.DIFF.6.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 6 & country == "Russia")$RU.DIFF.6.7)

issp_2007$RU.DIFF.8.7 <- issp_2007$RU.PRED.8*100 - issp_2007$RU.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 8 & country == "Russia")$RU.DIFF.8.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 8 & country == "Russia")$RU.DIFF.8.7)

issp_2007$RU.DIFF.9.7 <- issp_2007$RU.PRED.9*100 - issp_2007$RU.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 9 & country == "Russia")$RU.DIFF.9.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 9 & country == "Russia")$RU.DIFF.9.7)

issp_2007$RU.DIFF.10.7 <- issp_2007$RU.PRED.10*100 - issp_2007$RU.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 10 & country == "Russia")$RU.DIFF.10.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 10 & country == "Russia")$RU.DIFF.10.7)

issp_2007$RU.DIFF.11.7 <- issp_2007$RU.PRED.11*100 - issp_2007$RU.PRED.7*100
mean(subset(issp_2007, subset = sleep_duration == 11 & country == "Russia")$RU.DIFF.11.7)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 11 & country == "Russia")$RU.DIFF.11.7)

mean(subset(issp_2007, subset = sleep_duration == 7 & country == "Russia")$RU.PRED.7*100)

Country <- c("Russia", "Russia", "Russia", "Russia", "Russia", "Russia", "Russia", "Russia", "Russia")
Sleep <- c(3, 4, 5, 6, 7, 8, 9, 10, 11)
Mean <- c(0.3539733,
          0.2127005,
          0.1236595,
          0.04707689, NA, 
          -0.000420045,
          0.04349827,
          0.1179615,
          0.2232158)
SE <- c(0.01445387,
        0.008222096,
        0.003265568,
        0.000550868, NA, 
        4.22E-06,
        0.000557262,
        0.001896461,
        0.004749692)

RU.DIFFERENCES <- data.frame(Country, Sleep, Mean, SE)
RU.DIFFERENCES$ci.l.99 <- RU.DIFFERENCES$Mean - 2.576*RU.DIFFERENCES$SE 
RU.DIFFERENCES$ci.u.99 <- RU.DIFFERENCES$Mean + 2.576*RU.DIFFERENCES$SE 
RU.DIFFERENCES$ci.l.95 <- RU.DIFFERENCES$Mean - 1.96*RU.DIFFERENCES$SE 
RU.DIFFERENCES$ci.u.95 <- RU.DIFFERENCES$Mean + 1.96*RU.DIFFERENCES$SE 

View(RU.DIFFERENCES)

PLOT.RU.DIFFERENCES <- ggplot(RU.DIFFERENCES, aes(x=Sleep, y = Mean)) +
  geom_point(position=position_dodge(width=1), size=3, colour="black")  +
  geom_errorbar(aes(ymax = ci.u.99, ymin = ci.l.99), width = 0, position=position_dodge(width=1), colour="black", size=0.5) +
  geom_errorbar(aes(ymax = ci.u.95, ymin = ci.l.95), width = 0, position=position_dodge(width=1), colour="black", size=1.5) +
  theme_classic() +
  theme(axis.text.x=element_text(colour="black", size = 12), axis.text.y=element_text(colour="black", size = 12)) +
  theme(axis.title=element_text(size=14, colour="black", face="bold"), legend.text=element_text(size=14), legend.title=element_text(size=14)) +
  labs(    x = "\nSleep Duration (in Hours)\n", 
           y = "\n Differences (%) in the Predicted Probability of \nTurnout \n from the Peak Sleep Duration (7 Hours)\n",
           title = "") +
  geom_hline(yintercept = 0, size = I(0.3), color = I("black"), linetype="dashed") +
  ggpubr::grids(linetype = "dashed") +
  scale_x_continuous(breaks=c(3, 4, 5, 6, 7, 8, 9, 10, 11)) +
  scale_y_continuous(breaks=c(-9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1), limits = c(-9, 1)) +
  theme(strip.text.x = element_text(size = 12, colour = "black")) +
  annotate("rect", xmin = 6, xmax = 10, ymin = -Inf, ymax = Inf,
           alpha = .1,fill = "blue") +
  annotate("rect", xmin = -Inf, xmax = Inf, ymin = 1, ymax = Inf,
           alpha = .3, fill = "grey") +
  geom_vline(xintercept = 7, size = I(0.3), color = I("black"), linetype="solid") + 
  geom_text(data = data.frame(x = 8.75, y = 1, label = "the Average Predicted Probability \nat 7 Hours of Sleep is 98%"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE, fontface = 2) +
  geom_text(data = data.frame(x = 3.55, y = 0.35987970, label = "0.35"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 4.55, y = 0.21, label = "0.21"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 5.55, y = 0.12, label = "0.12"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 6.55, y = 0.09, label = "0.05"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 8.55, y = -0.09, label = "-0.0004"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 9.55, y = 0.09, label = "0.04"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 10.55, y = 0.12343660, label = "0.12"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 11.55, y = 0.22331280, label = "0.22"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE)

PLOT.RU.DIFFERENCES

subset(issp_2007, subset = sleep_duration == 3 & country == "South Korea", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 4 & country == "South Korea", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 5 & country == "South Korea", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 6 & country == "South Korea", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 7 & country == "South Korea", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 8 & country == "South Korea", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 9 & country == "South Korea", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 10 & country == "South Korea", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))
subset(issp_2007, subset = sleep_duration == 11 & country == "South Korea", select = c("sleep_duration_beta", "sleep_duration_power2_beta"))


issp_2007$SK.PRED.3 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -1.17,
                                                                 sleep_duration_power2_beta = -0.846,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="South Korea",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$SK.PRED.4 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -0.867,
                                                                 sleep_duration_power2_beta = -0.703,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="South Korea",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$SK.PRED.5 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -0.568,
                                                                 sleep_duration_power2_beta = -0.520,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="South Korea",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$SK.PRED.6 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = -0.270,
                                                                 sleep_duration_power2_beta = -0.295,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="South Korea",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$SK.PRED.7 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta =  0.0285,
                                                                 sleep_duration_power2_beta = -0.0301,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="South Korea",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$SK.PRED.8 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = 0.327 ,
                                                                 sleep_duration_power2_beta = 0.276,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="South Korea",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$SK.PRED.9 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                 sleep_duration_beta = 0.626,
                                                                 sleep_duration_power2_beta = 0.623,
                                                                 urban_rural_area_beta = 0,
                                                                 age_beta  = 0, 
                                                                 degree_beta  = 0,
                                                                 income_beta   = 0,
                                                                 sex_beta   = 0,
                                                                 political_interest_beta   = 0,
                                                                 religiosity_beta = 0,
                                                                 country="South Korea",
                                                                 local_election=0,
                                                                 mid_field_season=0,
                                                                 day_off=0), type = "response", allow.new.levels=T)

issp_2007$SK.PRED.10 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                  sleep_duration_beta = 0.924,
                                                                  sleep_duration_power2_beta = 1.01,
                                                                  urban_rural_area_beta = 0,
                                                                  age_beta  = 0, 
                                                                  degree_beta  = 0,
                                                                  income_beta   = 0,
                                                                  sex_beta   = 0,
                                                                  political_interest_beta   = 0,
                                                                  religiosity_beta = 0,
                                                                  country="South Korea",
                                                                  local_election=0,
                                                                  mid_field_season=0,
                                                                  day_off=0), type = "response", allow.new.levels=T)

issp_2007$SK.PRED.11 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                  sleep_duration_beta = 1.22,
                                                                  sleep_duration_power2_beta = 1.44,
                                                                  urban_rural_area_beta = 0,
                                                                  age_beta  = 0, 
                                                                  degree_beta  = 0,
                                                                  income_beta   = 0,
                                                                  sex_beta   = 0,
                                                                  political_interest_beta   = 0,
                                                                  religiosity_beta = 0,
                                                                  country="South Korea",
                                                                  local_election=0,
                                                                  mid_field_season=0,
                                                                  day_off=0), type = "response", allow.new.levels=T)

issp_2007$SK.DIFF.3.6 <- issp_2007$SK.PRED.3*100 - issp_2007$SK.PRED.6*100
mean(subset(issp_2007, subset = sleep_duration == 3 & country == "South Korea")$SK.DIFF.3.6)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 3 & country == "South Korea")$SK.DIFF.3.6)

issp_2007$SK.DIFF.4.6 <- issp_2007$SK.PRED.4*100 - issp_2007$SK.PRED.6*100
mean(subset(issp_2007, subset = sleep_duration == 4 & country == "South Korea")$SK.DIFF.4.6)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 4 & country == "South Korea")$SK.DIFF.4.6)

issp_2007$SK.DIFF.5.6 <- issp_2007$SK.PRED.5*100 - issp_2007$SK.PRED.6*100
mean(subset(issp_2007, subset = sleep_duration == 5 & country == "South Korea")$SK.DIFF.5.6)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 5 & country == "South Korea")$SK.DIFF.5.6)

issp_2007$SK.DIFF.7.6 <- issp_2007$SK.PRED.7*100 - issp_2007$SK.PRED.6*100
mean(subset(issp_2007, subset = sleep_duration == 7 & country == "South Korea")$SK.DIFF.7.6)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 7 & country == "South Korea")$SK.DIFF.7.6)

issp_2007$SK.DIFF.8.6 <- issp_2007$SK.PRED.8*100 - issp_2007$SK.PRED.6*100
mean(subset(issp_2007, subset = sleep_duration == 8 & country == "South Korea")$SK.DIFF.8.6)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 8 & country == "South Korea")$SK.DIFF.8.6)

issp_2007$SK.DIFF.9.6 <- issp_2007$SK.PRED.9*100 - issp_2007$SK.PRED.6*100
mean(subset(issp_2007, subset = sleep_duration == 9 & country == "South Korea")$SK.DIFF.9.6)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 9 & country == "South Korea")$SK.DIFF.9.6)

issp_2007$SK.DIFF.10.6 <- issp_2007$SK.PRED.10*100 - issp_2007$SK.PRED.6*100
mean(subset(issp_2007, subset = sleep_duration == 10 & country == "South Korea")$SK.DIFF.10.6)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 10 & country == "South Korea")$SK.DIFF.10.6)

issp_2007$SK.DIFF.11.6 <- issp_2007$SK.PRED.11*100 - issp_2007$SK.PRED.6*100
mean(subset(issp_2007, subset = sleep_duration == 11 & country == "South Korea")$SK.DIFF.11.6)
plotrix::std.error(subset(issp_2007, subset = sleep_duration == 11 & country == "South Korea")$SK.DIFF.11.6)

mean(subset(issp_2007, subset = sleep_duration == 6 & country == "South Korea")$SK.PRED.6*100)

Country <- c("South Korea", "South Korea", "South Korea", "South Korea", "South Korea", "South Korea", "South Korea", "South Korea", "South Korea")
Sleep <- c(3, 4, 5, 6, 7, 8, 9, 10, 11)
Mean <- c(-0.3392432,
          -0.02506781,
          0.08591768, NA, 
          -0.2699551,
          -0.7430802,
          -1.438319,
          -2.379962,
          -3.636011)
SE <- c(0.001335125,
        6.57E-05,
        0.000133668, NA, 
        0.000315599,
        0.00115135,
        0.003545055,
        0.01139415,
        0.01996462)

SK.DIFFERENCES <- data.frame(Country, Sleep, Mean, SE)
SK.DIFFERENCES$ci.l.99 <- SK.DIFFERENCES$Mean - 2.576*SK.DIFFERENCES$SE 
SK.DIFFERENCES$ci.u.99 <- SK.DIFFERENCES$Mean + 2.576*SK.DIFFERENCES$SE 
SK.DIFFERENCES$ci.l.95 <- SK.DIFFERENCES$Mean - 1.96*SK.DIFFERENCES$SE 
SK.DIFFERENCES$ci.u.95 <- SK.DIFFERENCES$Mean + 1.96*SK.DIFFERENCES$SE 

View(SK.DIFFERENCES)

PLOT.SK.DIFFERENCES <- ggplot(SK.DIFFERENCES, aes(x=Sleep, y = Mean)) +
  geom_point(position=position_dodge(width=1), size=3, colour="black")  +
  geom_errorbar(aes(ymax = ci.u.99, ymin = ci.l.99), width = 0, position=position_dodge(width=1), colour="black", size=0.5) +
  geom_errorbar(aes(ymax = ci.u.95, ymin = ci.l.95), width = 0, position=position_dodge(width=1), colour="black", size=1.5) +
  theme_classic() +
  theme(axis.text.x=element_text(colour="black", size = 12), axis.text.y=element_text(colour="black", size = 12)) +
  theme(axis.title=element_text(size=14, colour="black", face="bold"), legend.text=element_text(size=14), legend.title=element_text(size=14)) +
  labs(    x = "\nSleep Duration (in Hours)\n", 
           y = "\n Differences (%) in the Predicted Probability of \nTurnout \n from the Peak Sleep Duration (6 Hours)\n",
           title = "") +
  geom_hline(yintercept = 0, size = I(0.3), color = I("black"), linetype="dashed") +
  ggpubr::grids(linetype = "dashed") +
  scale_x_continuous(breaks=c(2, 3, 4, 5, 6, 7, 8, 9, 10, 11)) +
  scale_y_continuous(breaks=c(-9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1), limits = c(-9, 1)) +
  theme(strip.text.x = element_text(size = 12, colour = "black")) +
  annotate("rect", xmin = 6, xmax = 10, ymin = -Inf, ymax = Inf,
           alpha = .1,fill = "blue") +
  annotate("rect", xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = -1,
           alpha = .3, fill = "grey") +
  geom_vline(xintercept = 6, size = I(0.3), color = I("black"), linetype="solid") +
  geom_text(data = data.frame(x = 8, y = 1, label = "the Average Predicted Probability \nat 6 Hours of Sleep is 87%"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE, fontface = 2) + 
  geom_text(data = data.frame(x = 3.55, y = -0.34298200, label = "-0.34"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 4.55, y = -0.09, label = "-0.03"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 5.55, y = 0.1, label = "0.09"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) + 
  geom_text(data = data.frame(x = 7.55, y = -0.27, label = "-0.27"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 8.55, y = -0.74, label = "-0.74"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 9.55, y = -1.44, label = "-1.44"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) + 
  geom_text(data = data.frame(x = 10.55, y = -2.38, label = "-2.38"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE) +
  geom_text(data = data.frame(x = 11.55, y = -3.64, label = "-3.64"),
            mapping = aes(x = x, y = y, label = label),
            size = 4, inherit.aes = FALSE)

PLOT.SK.DIFFERENCES

diff_figures <- ggpubr::ggarrange(PLOT.FI.DIFFERENCES,
                                  PLOT.GR.DIFFERENCES,
                                  PLOT.IE.DIFFERENCES,
                                  PLOT.MX.DIFFERENCES,
                                  PLOT.NL.DIFFERENCES,
                                  PLOT.NZ.DIFFERENCES,
                                  PLOT.PH.DIFFERENCES,
                                  PLOT.SK.DIFFERENCES,
                                  PLOT.RU.DIFFERENCES,
                                  ncol=3, nrow = 3, labels = c("A: Finland", 
                                                               "B: Greece",
                                                               "C: Ireland", 
                                                               "D: Mexico", 
                                                               "E: the Netherlands",
                                                               "F: New Zealand",
                                                               "G: the Philippines",
                                                               "H: South Korea",
                                                               "I: Russia"))
diff_figures

ggsave(filename = "diff_figures.jpg",plot = diff_figures, width = 28, height = 16, dpi = 1000)

table(issp_2007$income_beta, exclude = NULL)
table(issp_2007$income, exclude = NULL)


issp_2007$INC.PRED.1 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                  sleep_duration_beta = 0,
                                                                  sleep_duration_power2_beta =  0,
                                                                  urban_rural_area_beta = 0,
                                                                  age_beta  = 0, 
                                                                  degree_beta  = 0,
                                                                  income_beta   = -0.745046260699861,
                                                                  sex_beta   = 0,
                                                                  political_interest_beta   = 0,
                                                                  religiosity_beta = 0,
                                                                  country="Ireland",
                                                                  local_election=0,
                                                                  mid_field_season=0,
                                                                  day_off=0), type = "response", allow.new.levels=T)

issp_2007$INC.PRED.2 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                  sleep_duration_beta = 0,
                                                                  sleep_duration_power2_beta =  0,
                                                                  urban_rural_area_beta = 0,
                                                                  age_beta  = 0, 
                                                                  degree_beta  = 0,
                                                                  income_beta   = -0.382475356036771,
                                                                  sex_beta   = 0,
                                                                  political_interest_beta   = 0,
                                                                  religiosity_beta = 0,
                                                                  country="Ireland",
                                                                  local_election=0,
                                                                  mid_field_season=0,
                                                                  day_off=0), type = "response", allow.new.levels=T)

issp_2007$INC.PRED.3 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                  sleep_duration_beta = 0,
                                                                  sleep_duration_power2_beta =  0,
                                                                  urban_rural_area_beta = 0,
                                                                  age_beta  = 0, 
                                                                  degree_beta  = 0,
                                                                  income_beta   = -0.0199044513736805,
                                                                  sex_beta   = 0,
                                                                  political_interest_beta   = 0,
                                                                  religiosity_beta = 0,
                                                                  country="Ireland",
                                                                  local_election=0,
                                                                  mid_field_season=0,
                                                                  day_off=0), type = "response", allow.new.levels=T)

issp_2007$INC.PRED.4 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                  sleep_duration_beta = 0,
                                                                  sleep_duration_power2_beta =  0,
                                                                  urban_rural_area_beta = 0,
                                                                  age_beta  = 0, 
                                                                  degree_beta  = 0,
                                                                  income_beta   = 0.34266645328941,
                                                                  sex_beta   = 0,
                                                                  political_interest_beta   = 0,
                                                                  religiosity_beta = 0,
                                                                  country="Ireland",
                                                                  local_election=0,
                                                                  mid_field_season=0,
                                                                  day_off=0), type = "response", allow.new.levels=T)

issp_2007$INC.PRED.5 <- predict(poly.issp.m1, newdata = transform(issp_2007, 
                                                                  sleep_duration_beta = 0,
                                                                  sleep_duration_power2_beta =  0,
                                                                  urban_rural_area_beta = 0,
                                                                  age_beta  = 0, 
                                                                  degree_beta  = 0,
                                                                  income_beta   = 0.7052373579525,
                                                                  sex_beta   = 0,
                                                                  political_interest_beta   = 0,
                                                                  religiosity_beta = 0,
                                                                  country="Ireland",
                                                                  local_election=0,
                                                                  mid_field_season=0,
                                                                  day_off=0), type = "response", allow.new.levels=T)


issp_2007$INC.DIFF.1.3 <- issp_2007$INC.PRED.1*100 - issp_2007$INC.PRED.3*100
issp_2007$INC.DIFF.2.3 <- issp_2007$INC.PRED.2*100 - issp_2007$INC.PRED.3*100
issp_2007$INC.DIFF.4.3 <- issp_2007$INC.PRED.4*100 - issp_2007$INC.PRED.3*100
issp_2007$INC.DIFF.5.3 <- issp_2007$INC.PRED.5*100 - issp_2007$INC.PRED.3*100

mean(subset(issp_2007, subset = income == 1)$INC.PRED.3*100)

mean(subset(issp_2007, subset = income == 1)$INC.DIFF.1.3)
plotrix::std.error(subset(issp_2007, subset = income == 1)$INC.DIFF.1.3)
-5.786923 - 2.576*0.007001804
-5.786923 + 2.576*0.007001804

mean(subset(issp_2007, subset = income == 2)$INC.DIFF.2.3)
plotrix::std.error(subset(issp_2007, subset = income == 2)$INC.DIFF.2.3)
-2.837225 - 2.576*0.003160179
-2.837225 + 2.576*0.003160179

mean(subset(issp_2007, subset = income == 4)$INC.DIFF.4.3)
plotrix::std.error(subset(issp_2007, subset = income == 4)$INC.DIFF.4.3)
2.679215 - 2.576*0.003477043
2.679215 + 2.576*0.003477043

mean(subset(issp_2007, subset = income == 5)$INC.DIFF.5.3)
plotrix::std.error(subset(issp_2007, subset = income == 5)$INC.DIFF.5.3)
5.210545 - 2.576*0.00727695
5.210545 + 2.576*0.00727695

#### Online Appendices, Research Note 9 ####

t.test.from.summary.data <- function(mean1, sd1, n1, mean2, sd2, n2, ...) {
  data1 <- scale(1:n1)*sd1 + mean1
  data2 <- scale(1:n2)*sd2 + mean2
  stats::t.test(data1, data2, ..., alternative = c("two.sided"), paired = F, var.equal = FALSE)
}

psych::describeBy(issp_2007$turnout, issp_2007$country)

t.test.from.summary.data(0.83, 0.38, 1174, 0.83, 0.37, 1281) # Finland: ISSP 2007 v. CSES Module 3
t.test.from.summary.data(0.72, 0.45, 1975, 0.88, 0.32, 1426) # Ireland: ISSP 2007 v. CSES Module 3
t.test.from.summary.data(0.73, 0.45, 1456, 0.85, 0.36, 1590) # Mexico: ISSP 2007 v. CSES Module 3
t.test.from.summary.data(0.82, 0.39, 1088, 0.86, 0.34, 1199) # the Philippines: ISSP 2007 v. CSES Module 2


